in reply to Re: Help in using arrays
in thread Help in using arrays

Your code suggestion:
while (<DATA>) { my $code = (split)[1]; for my $x (0..$#all) { if($code eq $all[$x]) { print $_; last; } } }
I suppose you have written it this way to stay close to the OP's coding style, but it should be mentioned that indexing the @all array is not necessary and not particularly perlish.
while (<DATA>) { my $code = (split)[1]; for my $x ( @all ) { if($code eq $x) { print $_; last; } } }
Now it becomes apparent that what the loop does is really a grep operation:
while (<DATA>) { my $code = (split)[1]; print if grep $_ eq $code, @all; } }
Finally, it turns out that grep can be replaced with a hash lookup when the list @all is replaced by a hash:
my %all = map +( $_ => 1), qw(7723 7725 9908 7765 7874); $all{ (split)[ 1]} and print while <DATA>;
Anno

Update: Stylistic change, pomposity tuned down a notch.

Replies are listed 'Best First'.
Re^3: Help in using arrays
by davorg (Chancellor) on Mar 14, 2007 at 15:08 UTC
      Just one small point. At the point when you switch to using "grep", you lose the tiny optimisation that you get from using "last" in the loop.

      Indeed. I thought of mentioning it, but that seemed to invite a deviation to List::Util::first and I dropped the whole thing.

      Anno