in reply to **HomeWork** Printing the found input from an array
Your issue is scoping.
foreach my $city ( @myCities ) { # $city is local to this loop only! } print "$city"; #there's no $city in scope!
You've declared $city within the scope of the foreach loop. Once you leave that loop, $city goes out of scope. If you'd used strict and warnings you would have gotten a warning on that print "main::city used only once", which would have been a good clue that you might have scope issues.
Simple fix? Move $city into a wider scope:
my $city; # now is scoped to the whole package! foreach $city ( @myCities ) { # do your stuff } print "$city"; # works as expected!
Make sense?
<–radiant.matrix–>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: **HomeWork** Printing the found input from an array
by pKai (Priest) on Apr 02, 2008 at 21:11 UTC | |
Re^2: **HomeWork** Printing the found input from an array
by ikegami (Patriarch) on Apr 02, 2008 at 20:58 UTC | |
Re^2: **HomeWork** Printing the found input from an array
by trenchwar (Beadle) on Apr 02, 2008 at 16:12 UTC | |
by CountZero (Bishop) on Apr 02, 2008 at 20:01 UTC |
In Section
Seekers of Perl Wisdom