deprecated has asked for the wisdom of the Perl Monks concerning the following question:
But, how do you decide when to add an arrayref or a scalar? I was thinking there should be a add-in-array-context operator for this, and I even vaguely recall that this was RFC'd, using :=. Now, granted, I hate Pascal, so I'm not keen on using that operator. I also recall that one of Larry's rules in Apocalypse 1 was "Larry Gets The Colon." So the add-in-array-context idea was shot down (i think). So instead of having my nifty operator, I have this less-than-slick code:my %statistics = ( 1 => [ "foo", "bar" ], 2 => [ "baz", "bletch" ], 3 => [ "quux" ], );
Which seems to be a) semi-fragile and b) inelegant. Note: sometimes we will hit a piece of the file that says "we dont need to know any more about this." So we stop parsing at that point. We also dont know how long (or short) these files are going to be. So we cant very well just populate the hash with array refs (which would be an ugly kludgeful way to do this, imho). Is there a smarter way to do this?# if we havent hit this line yet, add an array # reference to %statistics at this line number. if (not defined $statistics{$line_number} ) { $statistics{$line_number} = [ $thisline ]; } # if we have hit this line, then we have an # array reference already and all we have to do # is push it. if (defined $statistics{$line_number} ) { push @{ $statistics{$line_number} }, $thisline; }
brother dep
--
Laziness, Impatience, Hubris, and Generosity.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Larry Gets The Colon. (selective addition of an arrayref to a hash) (code)
by chipmunk (Parson) on Jun 01, 2001 at 18:26 UTC | |
|
Re: Larry Gets The Colon. (selective addition of an arrayref to a hash) (code)
by bwana147 (Pilgrim) on Jun 01, 2001 at 18:27 UTC | |
|
Re: Larry Gets The Colon. (selective addition of an arrayref to a hash) (code)
by bikeNomad (Priest) on Jun 01, 2001 at 18:30 UTC | |
by Vynce (Friar) on Jun 02, 2001 at 12:43 UTC | |
|
Re: Larry Gets The Colon. (selective addition of an arrayref to a hash) (code)
by ChemBoy (Priest) on Jun 01, 2001 at 19:13 UTC | |
by bwana147 (Pilgrim) on Jun 01, 2001 at 20:21 UTC | |
by Anonymous Monk on Oct 14, 2003 at 09:29 UTC |