I think this is to do with an interesting feature of the
x operator. The awfwy squoowy thing is happening right at the initialisation point:
my @data = ({}) x (3);
print Dumper \@data;
produces
$VAR1 = [
{},
$VAR1->[0],
$VAR1->[0]
];
... and not
$VAR1 = [
{},
{},
{},
];
- if it had done that (for which you need
@data = ({},{},{});) then the rest of the script would have done what you expected.
This tells something about how the
x operator treats hashrefs (and arrayrefs for that matter) - it sets up the whole data structure so that *all* the data structures are whatever you set any one of them to, last time you set them.
It's instructive to put a
last if $row == $n; at the end of your
foreach my $row.... Then look at the contents of
$report->{data} with
for (0..2) {
print $report->{data}->[$_]->{foo}," => ", $report->{data}->[$_]->
+{bar},"\n";
}
- and goldarn if there isn't always the same thing in each of the three hashes, albeit a *different* same thing depending on what
$n is.
Strikes me this could be a rather useful feature of the
x operator... if only I can remember it until the right moment arises.
Thanks very much for this stimulating thread, by the way.
(I think part of the reason it was hard to work out what was going on is that (IMHO)
local *MCVE::MCVE_NumRows = sub { 3 }; should be
local *MCVE::MCVE_NumRows = sub { 4 }; - I think it's meant to be all the rows *including* the headers. Making it one short means that your sub is actually creating *one* hash in
@data which is independent of the initialised hashes, and hence with a life of its own. That's why both the last *and* the second last key/value pairs in
@rows are represented.)
§
George Sherston
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.