Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: Surpised by foreach iterator limitation

by shotgunefx (Parson)
on Apr 08, 2003 at 03:06 UTC ( #248787=note: print w/replies, xml ) Need Help??


in reply to Re: Surpised by foreach iterator limitation
in thread Surpised by foreach iterator limitation

Umm.. I wasn't attempting to iterate through a hash. I was trying to extend a validation function that takes a hash as it's argument to accept multiple values in the laziest way possible. $hash{key} is a scalar variable and can be localized, so I assumed it could be used any place a scalar could which was the point of the post.

-Lee

"To be civilized is to deny one's nature."
  • Comment on Re: Re: Surpised by foreach iterator limitation

Replies are listed 'Best First'.
Re: Re: Re: Surpised by foreach iterator limitation
by pg (Canon) on Apr 08, 2003 at 03:12 UTC
    Okay, now I see, however, $hash{key} is even not a valid name for a scalar variable.br>
    Be careful about this subtle difference: $hash{key} is the valid syntax to point to a hash element, which is a scalar, but it is not the valid syntax to name a scalar.

      Note that you can do:

      local $hash{$key} = 1; local $array[$index] = 3;

      Since foreach() works a lot more like local() than my(), there is no reason why it should not work, other than the fact that it currently does not. Somebody suitably motivated could make a patch to get this to work, and with enough convincing, the perl5-porters may accept it into mainstream perl 5.10.

        Wrong.

        use Data::Dumper; %hash = (key => 1); local $hash{key} = 2; print Dumper(\%hash);# print 2 print $hash{key};# also print 2.
        This clearly demos that $hash{key} is not another scalar, it simply refers to a hash element, which is a scalar. Again, please try to understand the subtle difference between "valid syntax to name an element of a collective, which happened to be a scalar", and "valid syntax for scalar name".

        They will support it in Perl 5.10? Hope this will never happen ;-)

        Let's look at from a different perspective. In c, you can say,
        int a[200];
        You can also say,
        a[100] = 2;
        but is a[100] a valid variable name? no, it is NOT. It can be used to call an array element, but it is not a valid variable name. It is the same situation here.
      But it is a valid scalar value which was why I was suprised.

      -Lee

      "To be civilized is to deny one's nature."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2020-02-18 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (75 votes). Check out past polls.

    Notices?