is replacing the original array_ref value of the hash element with a scalar value -- in other words, where $array_of_lists{$key} was originally initialized as a reference to an array, the statement above is assigning a scalar value to that hash element (the last array value returned by splice, which in this case happens to be the only array value returned by splice). Then, in a later iteration of one of those for loops, you are trying to use that hash element as if it were still a reference to an array (but it isn't anymore).$array_of_lists{$key} = splice(@{$array_of_lists{$key}}, $t, 1);
If you wanted that assignment to preserve the hash value as an array ref, you would have to do it like this:
@{$hash_of_arrays{$key}} = splice( ... );
There are other problems with this version of your code, involving the for loops. Instead of this:
You should be doing something like this:my ($key, $t); for $key ( 0 .. scalar(keys %array_of_lists)) { for $t ( 0 .. scalar @{$array_of_lists{$key}} ) {
where the "$#" sigil represents the index number of the last element in the array (i.e. "$#array" or "$#{$array_ref}" or "$#{$hash_of_arefs{$key}}"). Note that your version of the for loops will both do one iteration too many (and the outer loop wouldn't work at all if the hash keys were not integers that increment from 0).for my $key ( sort {$a<=>$b} keys %hash_of_lists ) { for my $t ( 0 .. $#{$hash_of_lists{$key}} ) {
Note also the use of sort for ordering the hash keys; that usage of $a and $b (globals that are always declared by perl) is the reason why you don't use those variable names for other things.
The commentary in your code is not at all clear -- I have no idea what you are really trying to accomplish -- so I can't tell whether your use of splice is appropriate. (I don't use splice much, because I usually find simpler and clearer ways of reducing arrays -- IMHO, splice is a relatively complicated tool, tending to make its users more prone to mistakes that may be hard to debug. The suggestion below from GrandFather about using grep is a good one.)
Another point to learn from GrandFather's reply: post a self-contained piece of code that other monks can actually run, without having to make up input data on their own. It really helps to have a clear idea about what sort of data you are trying to handle.
Last nit-pick about improving the comments: since the terms you are using for your variable names are vague, your comments should specifically refer to particular variable names when describing what they contain and how they interact in the code. And/or you could try using variable names that are more clearly descriptive of what they are for. A step-wise synopsis of the overall procedure/goal of the script would also be helpful -- POD is good for that -- if you can't describe this clearly, how could you expect to write code for it?
In reply to Re^3: working with hashes of arrays
by graff
in thread working with hashes of arrays
by gman1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |