Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: A question of perlish elegance on array loop

by ait (Hermit)
on Nov 30, 2007 at 00:44 UTC ( [id://654018]=note: print w/replies, xml ) Need Help??


in reply to Re: A question of perlish elegance on array loop
in thread A question of perlish elegance on array loop

WOW! Many thanks, it works like a charm

Since perl it usually chokes (not an array ref on...) on object array references like so: foreach(@$obj->{array_ref}) I figured that it would also choke on $#{$obj->{array_ref}} by not recognizing the array ref in the inner reference. That's why I did'nt bother trying and following my instinct (which usually has worked in Perl!) went ahead on the use of $#{@{
But that looked pretty awkward so I went and searched a lot CPAN code to see if anyone else was using it, and well, you know the rest of the story :)

I guess I'll have to study a lot on Introspection and Globs to get the answer to that one, huh?

Update: Hmmm. After re-reading this, I think I have just answered myself: the same way that @{ "casts" to array is the same way $#{ "casts" to array also. Silly me! Thanks again for making me understand this!
  • Comment on Re^2: A question of perlish elegance on array loop

Replies are listed 'Best First'.
Re^3: A question of perlish elegance on array loop
by Fletch (Bishop) on Nov 30, 2007 at 00:52 UTC

    Not really globs, just references. In general wherever you can have a sigil ($@%*) followed by a variable name you can use a BLOCK returning a reference of the appropriate type; e.g. just like @hash{ qw/x y/ } does a hash slice of %hash you can do @{ $hashref }{ qw/ x y / } to slice the hash referred to by $hashref.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found