in reply to print array of hashes
how to print this structure?How can I visualize my complex data structure?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: print array of hashes
by fionbarr (Friar) on Apr 22, 2010 at 14:29 UTC | |
both these work...would someone be so kind as to explain each of them? | [reply] [d/l] |
by choroba (Cardinal) on Apr 22, 2010 at 14:46 UTC | |
| [reply] [d/l] [select] |
by almut (Canon) on Apr 22, 2010 at 16:05 UTC | |
And scalar applied to a list returns the number of elements in the list. That statement could be misleading...:
A list (as opposed to an array) in scalar context evaluates to its last element. | [reply] [d/l] [select] |
by Anonymous Monk on Apr 22, 2010 at 17:02 UTC | |
| [reply] |
by Marshall (Canon) on Apr 22, 2010 at 17:38 UTC | |
You have an array of "pointers" to anon hashes. Data::Dumper is a SUPER cool thing to help visualize what is going on.
Below, I have a foreach loop that processes every hash_ref in @rec. An array in a scalar context is the number of things in that array. I think others have covered that point.
| [reply] [d/l] [select] |
by dmlond (Acolyte) on Apr 22, 2010 at 20:18 UTC | |
There are a couple of things going on here. First, you have the difference between: and Here the parentheses act like they do in basic math, so, in this case, the two operations are identical. Next you have the difference between: and For many perl functions (such as scalar), the parentheses surrounding the arguments are optional. It often reads more like a natural human sentence if you omit them, but often it is better to explicitly include them if the resulting statement does not read well as a natural human sentence. You might also want to know what @{EXPRESSION} means. This is how to dereference an array reference in perl. If $val holds a reference to an array (e.g. it does not hold the array itself), then @{$val} returns the actual array. Similarly you can use %{$val} to dereference the hash referenced by $val, ${$val} to dereference the scalar referenced by $val, and &{$val} to dereference the subroutine referenced by $val, with @{$val}(ARGS) if you need to pass arguments. Finally, there is the difference between: and Each element in the @rec array is itself a reference to a hash. Perl provides these two ways to access the scalar held as a value in a hashref, which only differ in the use of the '->' operator. I recommend always using the '->' operator, which explicitly means 'the thing referenced by'. It is much more self documenting. $rec[0]->{kids} is the value keyed to 'kids' in the hash referenced by $rec[0]. Similarly, $array_ref->[0] is the first element in the array referenced by $array_ref, and $code_ref->() is the subroutine referenced by $code_ref. Also, $code_ref->(ARGS) is much more pretty than @{$code_ref}(ARGS). When you get into multidimensional data structures, the '->' can be very helpful. Compare the identically functioning: and The last one is much more self-documenting, and really starts to look more like an object (which also uses '->' for attributes and methods, which should give you the impression that it is pretty powerful magic). | [reply] [d/l] [select] |