Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Surpised by foreach iterator limitation

by pg (Canon)
on Apr 08, 2003 at 03:00 UTC ( #248786=note: print w/replies, xml ) Need Help??


in reply to Surpised by foreach iterator limitation

Just to recap my main point thru out this subthread: (many points here is not directly against the original post, but against the whole subthread. Many of those things I disagree with is even not part of shotgunefx's original purpose, but MarkM's extension. I disagree with MarkM's points, but not him as a fellow monk ;-)

$hash{key} is the valid syntax to name a hash element, which happened to be a scalar (which could be array ref, hash ref ...), does not mean $hash{key} is a valid variable name for a scalar.

There is something subtle here, and we are looking at two different perspectives.

maybe a better way to explain this subtle difference is to look at some other computer language, for example c:

You can define a collective int a[200];, and ref to its 100th element by saying a[99], but a[99] is not a valid variable name. Otherwise, how can you recognize whether a[99] is a single variable or an element of a collective data structure?

One may say that this is Perl, not c. Well, when you look at the foundation, they have more similarities than differences. Whatever language it is, it requires the syntax can be consistently parsed/recognized.

Some people may argue that there are lots of "bad" syntax in Perl, what a big deal to have one more? Again, here is something subtle. Perl might have many "bad" syntax from a conventional language point of view, but all those "bad" syntax can be consistently parsed according to context. I bet nobody dare to have "bad" syntax that cannot be consistently parsed.

(the original content of this original reply is based on misunderstanding of what shotgunefx means. He actually means something much more subtle.)I don't call that a limitation ;-). To iterate thru a hash, you would do something like:
%hash = (a=>1, b=> 2, c=>3); foreach (keys %hash) { print "\$hash{$_} = $hash{$_}\n"; }
What makes this conventional approach not work for you?

Replies are listed 'Best First'.
Re: Re: Surpised by foreach iterator limitation
by shotgunefx (Parson) on Apr 08, 2003 at 03:06 UTC
    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."
      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.

        But it is a valid scalar value which was why I was suprised.

        -Lee

        "To be civilized is to deny one's nature."
Re: Re: Surpised by foreach iterator limitation
by shotgunefx (Parson) on Apr 08, 2003 at 04:26 UTC
    I certainly wasn't suggesting that $hash{key} be a valid scalar name. I simply and mistakenly thought that any lvalue would work in the iterator position.

    -Lee

    "To be civilized is to deny one's nature."
      Seriously, what you suggested here makes sense to me, but I think it is just the syntax, and the parser requires a valid variable name here, (or if it is missing, $_ is assumed).

Log In?
Username:
Password:

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

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










    Results (98 votes). Check out past polls.

    Notices?