Garanixx has asked for the wisdom of the Perl Monks concerning the following question:

hello everyone i have a question about a program i have tried to write, i am a beginner to programming overall, and i am having much trouble with everything,including syntax errors and the like, but in this case,none are reported, even when using -w , it is a basic random password generator i tried to create for myself,as you can see,i am using the urandom file that is included with linux, the matching does not work, no matter what i put inbetween the m// function,it will push every character into the array, while i only want it to push 0-9 and a-z,i tried manually specifying /55-90/ also with a backslash /\55-90/ (can't use the [] parenthesis since it creates links,but in my program i have it) and just to try,i tried using the defined \w switch ,and nothing works.. thank you in advance for you reply
#!/usr/bin/perl -w $DEBUG=1; #this is my random password generator project open(FILE ,"/dev/urandom") or die "could not open the file: $!"; $index=0; print "how many characters in length should the password be?\n"; $input=<STDIN>; chomp($input); while($index < $input) { $character=getc FILE; chomp($character); print "### DEBUG: \$character is $character\n" if $DEBUG; if(chr $character =~ m/[55-90]/) { push(@array ,$character); $index++; } } print reverse(@array); close FILE;
UPDATE:I was making quite the bad mistakes i see.. i did check the documentation, but unfortunately i didnt grasp it too well, its just that i'm only about 2 weeks into programming and all of this still hurts my head a little.. i will learn, thank you all very much for your replies

20050209 Edit by castaway: Changed title from 'Why won't this program work?'

Replies are listed 'Best First'.
Re: Matching alphanumeric characters from urandom
by VSarkiss (Monsignor) on Feb 07, 2005 at 21:03 UTC

    In addition to the good answer about ord above, you also need to realize that m/[55-90]/ isn't doing what you want. A bracketed construct matches exactly one character, one of the ones in its range. For example, m/[78!]/ would match any of the characters 7, 8 or !, but not the whole string 78!. Similarly, m/[5-9]/ would match any one of the characters 5, 6, 7, 8, or 9, but a construct like m/[55-90]/ is just confusing.

    If you want to check whether the character is ascii, you need to see if it's within the range you want. Perl lets you check the character directly, like so:

    if ($character >= 'A' && $character <= 'Z')
    is a reasonable test, as long as you're sure your input is ASCII (it wouldn't work for EBCDIC, as an extreme example).

Re: Matching alphanumeric characters from urandom
by RazorbladeBidet (Friar) on Feb 07, 2005 at 20:52 UTC
    I think you want "ord", not chr, right? You want the ASCII value of a character? ...
    my $char_num = ord( $character ); if ( $char_num >= 55 && $char_num <= 90 ) {
    ... or
    if ( $character =~ /[0-9a-z]/ ) { ... }
Re: Matching alphanumeric characters from urandom
by cowboy (Friar) on Feb 07, 2005 at 20:57 UTC
    Your pattern match, is matching:
    5, 5-9 (5,6,7,8,9), and 0
    Many characters will have chr values that match that. Try this:
    if ($character =~ m/^[a-z0-9]$/) { ... }
      Alternately, that can be rewritten as one of the following:
      • if ($character =~ /[a-z\d]/)
      • my %is_legal = map { $_ => ~~1 } 'a' .. 'z', 0 .. 9; if ($is_legal{ $character })
      • if ($character !~ /\W/ && $character ne '_' && lc $character eq $character)
      • if ($character =~ /\w/ && $character ne '_' && lc $character eq $character)

      (The latter two aren't really serious ...)

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Matching alphanumeric characters from urandom
by Fletch (Bishop) on Feb 07, 2005 at 21:01 UTC

    You're having problems with what exactly character classes mean. Check out perldoc perlretut and perldoc perlre, but just as a hint you probably wanted m/[[:alpha:]]/.