Home > Archive > PERL Beginners > September 2006 > Need help!
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| oldfrau 2006-09-04, 9:57 pm |
| it is my code:
#! /usr/bin/perl -w
use strict;
my(@array,$match,$item,$found);
@array=(-2,-3,-4,-4,5);
foreach $item (@array) {
if ($item>0) {
print ref($item);
$match = $item;
$found = 1;
last;
}
}
if ($found) {
print "the first positive item found in this array is:$match";
####atteniton###the line is here###
} else {
print "unfound";
}
and the code works quite well and it will print "the first positive
item found in this array is 5",but if i change the $match into $item
in the print line,i will get a problem. it seems that $item is
undef.but i think $item should be 5 too , because "last" stop the loop
and let $item equals 5 ..
puzzled!
| |
| Paul Lalli 2006-09-04, 9:57 pm |
| oldfrau wrote:
> it is my code:
> #! /usr/bin/perl -w
> use strict;
> my(@array,$match,$item,$found);
> @array=(-2,-3,-4,-4,5);
> foreach $item (@array) {
> if ($item>0) {
> print ref($item);
> $match = $item;
> $found = 1;
> last;
> }
> }
> if ($found) {
> print "the first positive item found in this array is:$match";
> ####atteniton###the line is here###
> } else {
> print "unfound";
> }
> and the code works quite well and it will print "the first positive
> item found in this array is 5",but if i change the $match into $item
> in the print line,i will get a problem. it seems that $item is
> undef.but i think $item should be 5 too , because "last" stop the loop
> and let $item equals 5 ..
> puzzled!
Unfortunately, there is something going on behind the scenes here.
Perl implicitly localises your iterator variable for the duration of
the loop. That is, it automatically saves off the value the variable
has before the loop, and restores that value at the end of the loop.
This is explained in `perldoc perlsyn`:
========================================
====
The "foreach" loop iterates over a normal list value and
sets the variable VAR to be each element of the list in
turn. If the variable is preceded with the keyword "my",
then it is lexically scoped, and is therefore visible only
within the loop. Otherwise, the variable is implicitly
local to the loop and regains its former value upon exiting
the loop. If the variable was previously declared with
"my", it uses that variable instead of the global one, but
it's still localized to the loop. This implicit
localisation occurs only in a "foreach" loop.
========================================
=====
By the way, in your case, I recommend using List::MoreUtils's
firstval() function:
use List::MoreUtils qw/firsvalt/;
my $item = firstval { $_ > 0 } @array;
Paul Lalli
| |
| DJ Stunks 2006-09-05, 3:57 am |
| Paul Lalli wrote:
> By the way, in your case, I recommend using List::MoreUtils's
> firstval() function:
>
> use List::MoreUtils qw/firsvalt/;
now _that's_ a typo... | ^
Paul, you must be hittin' | |
120 wpm :)~ +--+
> my $item = firstval { $_ > 0 } @array;
|
|
|
|
|