Beginner Perl help Please

Discussion in 'Perl' started by srijithbhandary, Apr 8, 2011.

  1. srijithbhandary

    srijithbhandary New Member

    Joined:
    Apr 8, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    [FONT=Arial,Helvetica, sans-serif][FONT=Arial,Helvetica, sans-serif]Hi I am trying to generate random names using perl script.

    First let me post my code
    [/FONT]
    [/FONT]
    Code:
    [FONT=Arial,Helvetica, sans-serif][COLOR=black][FONT=Arial,Helvetica, sans-serif][COLOR=black]#!/usr/bin/perl 
    my $name; 
    my @charset = (('A'..'Z'), ('a'..'z')); 
    my $range = $#charset + 1; 
    print "Enter the limit\n"; 
    $lim = <STDIN>; 
     
    until ($lim == 0){ 
    for (1..8) { 
       $name .= $charset[int(rand($range))]; 
    } 
    print "$name\n"; 
    $lim = $lim - 1; 
    }
    [/COLOR][/FONT][/COLOR][/FONT]
    
    [FONT=Arial,Helvetica, sans-serif][FONT=Arial,Helvetica, sans-serif]So this code first asks the number of names that I want to generate after that it generates the number of names.

    My problem is I dont want it to generate the way it is doing now.
    here is the output if i give limit as 6

    PeTFDyCo
    PeTFDyCotpeZftCG
    PeTFDyCotpeZftCGCztEldLf
    PeTFDyCotpeZftCGCztEldLfdULnsqvx
    PeTFDyCotpeZftCGCztEldLfdULnsqvxhYNMboqM
    PeTFDyCotpeZftCGCztEldLfdULnsqvxhYNMboqMXIDiwnOw

    Here it generates 6 names, but I have two issues here

    1) I dont want the previous name to be appended(prefix) to the next name

    2) I want all the names to be of same length

    Please help me. Thank you.
    [/FONT]
    [/FONT]
     
  2. sreek

    sreek New Member

    Joined:
    Nov 15, 2010
    Messages:
    11
    Likes Received:
    2
    Trophy Points:
    0
    the letters are appended to $name through out the until loop. Thats why you are getting previous name to be appended and length of the name increases.

    add $name=""
    in the first line of the until loop
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my @charset = ( 'A' .. 'Z', 'a' .. 'z'  );
    print "Enter the limit : ";
    chomp(my $lim = <STDIN>);
    
    while ( $lim-- ) {
    	my $name;
    
        for ( 1 .. 8 ) {
            $name .= $charset[ rand(@charset) ];
        }
        print "$name\n";
    }
     
    Output:
    Code:
    [pradeep@home-dev test]# ./rand.pl
    Enter the limit : 6
    OKdCOZCg
    gKMrjpBf
    tCpcSrnZ
    ufnBdeEc
    UbwHQbzp
    eyYkaqKC
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice