Are these -l and -n flags specific to perl 5.6? I am using 5.005 and they
don't work as illustrated:

% perl -e 'while{print if(.45909592993094279021 < rand);}' db_mlb_undup.dat
syntax error at -e line 1, near "while{"
Execution of -e aborted due to compilation errors.
% cat db_mlb_undup.dat | perl -e 'while(<>){print if(.45909592993094279021 <
rand);}' | head -1
100000127|
% cat db_mlb_undup.dat | perl -el 'while(<>){print if(.45909592993094279021
< rand);}' | head -1
% cat db_mlb_undup.dat | perl -en 'print if(.45909592993094279021 < rand);'
| head -1
% 

Thanks.
~Jess

-----Original Message-----
From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 19, 2002 11:34 AM
To: Balint, Jess
Cc: '[EMAIL PROTECTED]'
Subject: Re: rand < vs rand >


On Jun 19, Balint, Jess said:

>cat db_sample.dat | perl -e 'while(<>){chomp;if( rand <
>..41441569872973300025 ){print"$_\n";}}' | wc -l
>Warning: Use of "rand" without parens is ambiguous at -e line 1.
>Unterminated <> operator at -e line 1.
>      0
>
>Thanks for any explanation.

You should ask the perl documentation first, since 'perldiag' contains
explanations for all warnings and errors.  The problem is that rand() CAN
accept an argument, which just might happen to come from an expression
like <FH>.

  $x = rand <FH>;

So Perl has no way of knowing whether or not you're about to use a
filehandle or if you wanted the less-than operator.  Thus, use of
"rand" without parens is ambiguous.

  $x = rand() < .5;

or

  $x = .5 > rand;

And your code, by the way, could be shortened a bit:

>cat db_sample.dat | perl -e 'while(<>){chomp;if( rand <
>..41441569872973300025 ){print"$_\n";}}' | wc -l

  perl -e 'print if .414415 > rand' filename | wc -l

or even:

  perl -lne '$c++ if .414415 > rand; END { print $c }' filename

Basically:

  1. 'perl -n' makes a 'while (<>) { ... }' loop
  2. 'perl -l' auto-chomp()s in conjunction with -n
  3. 'perl -l' adds a "\n" after every print()
  4. perl can take a filename as input to read (meaning no 'cat')
  5. don't bother PRINTING lines, just COUNT them and print the count

That's all for now.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to