Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: **HomeWork** Printing the found input from an array

by radiantmatrix (Parson)
on Apr 02, 2008 at 14:43 UTC ( #677973=note: print w/ replies, xml ) Need Help??


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


Comment on Re: **HomeWork** Printing the found input from an array
Select or Download Code
Re^2: **HomeWork** Printing the found input from an array
by trenchwar (Beadle) on Apr 02, 2008 at 16:12 UTC
    Makes perfect since... I was isolating $city inside the loop which is why it had no value, correct?
      which is why it had no value
      But it did have a value! But only within the loop and once you left the loop the variable got out scope and did not exist anymore.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^2: **HomeWork** Printing the found input from an array
by ikegami (Pope) on Apr 02, 2008 at 20:58 UTC

    Make sense?

    Not really. There are now two variables named city (one localized to the loop and a new one in the outer scope), and one of them is always undef.

    The purpose of the loop is to validate $inner_city, so why is $city being printed? Fix:

    foreach my $city ( @myCities ) { # $city is local to this loop only! } print "$inner_city";
Re^2: **HomeWork** Printing the found input from an array
by pKai (Priest) on Apr 02, 2008 at 21:11 UTC
    # works as expected!

    Which depends on you expectations:

    my @myCities = (1 .. 2); my $city = "before"; # now is scoped to the whole package! foreach $city ( @myCities ) { # do your stuff $city = "after"; } print "$city"; # works as expected! # ... unless you expected it to print "after" # ... actually it prints "before"

    This is a Perl peculiarity I was once also bitten by.

    Actually Perl will localize the $city for the scope of the for-loop. See perldoc perlsyn.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://677973]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-04-18 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls