chrism01 has asked for the wisdom of the Perl Monks concerning the following question:
I've got a nice one liner that loads a file of recs into a hash, each rec being of the format name:val, like so
However, I've got another prog I want to use on the same data format, but the data is being fed in one rec at a time, so I need to accumulate the data, conceptually like this%t_hash = map{ split(/:/, $_, 2) } <DATA_FILE>;
Obviously this doesn't work ("Can't modify private hash in addition (+) at ./t.pl line 250, near "$var1;" Execution of ./t.pl aborted due to compilation errors. ").%t_hash += map{ split(/:/, $_, 2) } $rec;
I've tried a few variations, but can't find the solution (which I'm sure must be possible).
Anybody got the answer?
Cheers
Chris
PS I'm just curious about a 1-liner, I know i could use 2 eg
($key, $val) = split(/:/, $rec, 2); $t_hash{$key} = $val;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Accumulating data in a hash using map{split()}
by merlyn (Sage) on May 03, 2007 at 22:59 UTC | |
|
Re: Accumulating data in a hash using map{split()}
by GrandFather (Saint) on May 03, 2007 at 23:11 UTC | |
|
Re: Accumulating data in a hash using map{split()}
by chrism01 (Friar) on May 03, 2007 at 23:36 UTC | |
by GrandFather (Saint) on May 04, 2007 at 00:25 UTC | |
by chrism01 (Friar) on May 03, 2007 at 23:41 UTC |