catfish1116 has asked for the wisdom of the Perl Monks concerning the following question:
I have this as my code
while ( (@k, @v) = each %hash) { print "@k => @v \n"; }
and this is my output
6 18 => 50 150 => 3 9 => 7 21 => 91 273 => 9 27 => 1 3 => 4 12 => 34 102 => 89 267 => 76 228 =>
What do I have to do to get it like 4 => 12 ? TIA The Catfish
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Output of hash
by AnomalousMonk (Archbishop) on Apr 23, 2019 at 22:35 UTC | |
stevieb++ and johngg++ have explained the problem and its solution, but note also that this feature (yes, feature) is called "list flattening". Any lvalue array (or hash) in a list assignment will consume all remaining items in the assigned (RHS) list. Try this with %y (a hash) in place of the @y array, and with any combination of scalar(s), array(s) or hash(s) in place of the @z array. BTW, in the category of Stupid Hash Tricks, try this:
Update: A solution using assignment to an array (update: could also be a hash %kv) (also not recommended) might be something like: (Update: In adding this Update, I screwed up the previous BTW example code. Everything's fixed now.) Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
|
Re: Output of hash
by stevieb (Canon) on Apr 23, 2019 at 21:25 UTC | |
In other words, each() returns two scalar items. What's happening in your case, is that @k is taking both items being returned for each hash key/value pair on each iteration, so there's nothing ever in the @v array. Use scalars like I have instead. | [reply] [d/l] [select] |
|
Re: Output of hash
by johngg (Canon) on Apr 23, 2019 at 21:27 UTC | |
You are using arrays @k and @v so both key and value go into @k and @v gets nothing. Use scalars $k and $v instead. Cheers, JohnGG | [reply] [d/l] [select] |
|
Re: Output of hash
by Marshall (Canon) on Apr 23, 2019 at 23:39 UTC | |
I think how to use each %hash has been well covered. However, I point out that iterating of the keys %hash is far, far more common. Usually what is desired is some kind of ordering of the printout, typically some kind of sorted order. (also shown below). Update: added another test case to show that both the key and the value windup in @k in this situation while ( my(@k, $v, $anything) = each %hash) Also worthy of note is that the order of the hash will vary between runs using recent Perl's. Run my code a couple of times and you will see. The function that calculates the internal binary hash key has a per run "fudge factor" - there are some security and other performance issues related to this behaviour. Bottom Line: Do not count on the each %hash function processing in the same order each run because it will not! This is one reason why using some kind of sort function upon keys is much,much more common (at least in my code) than using the each %hash function.
| [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on Apr 24, 2019 at 03:26 UTC | |
below keys and values "line up" but not sure that is guaranteed: I'm not sure just what that statement refers to, but the following identical language from keys/values/each may be pertinent: So long as a given hash is unmodified you may rely on keys, values and each to repeatedly return the same order as each other. See Algorithmic Complexity Attacks in perlsec for details on why hash order is randomized. [emphases added] Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
by Marshall (Canon) on Apr 25, 2019 at 00:18 UTC | |
values "line up" just means that key=>value correspondence between the horizontal presentation using values and keys vs the vertical presentation using each are the same.
Update: | [reply] |
by AnomalousMonk (Archbishop) on Apr 24, 2019 at 03:01 UTC | |
| [reply] [d/l] | |