Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

accessing an anonymous array

by WoodyWeaver (Monk)
on Apr 11, 2020 at 19:55 UTC ( #11115386=perlquestion: print w/replies, xml ) Need Help??

WoodyWeaver has asked for the wisdom of the Perl Monks concerning the following question:

pray help a poor wanderer with syntax

I am populating a table with some statistics, e.g.
$tables{$day}->{$state} = \[$deathsToday, $m, $double, $intercept, + $corr];
These are grouped in my mind as an array, so that's why I used the anonymous array above. So then it gets used later, viz
foreach my $point ( @${$tables{$day}->{$state}} ) { print $fh "\t", $point;
After time, I want to add a feature, for which I need to access just the first element. It seems like it should be something like ${$tables{$day}->{confirmed}}[0]; but that isn't right. dump $tables{$day} helpfully offers
{ confirmed => \[1, 0.334638642561918, 2, 0.873307778517249, 0.8539438 +57158699], deaths => \[], }
(This is a covid visualization thing.) I could of course convert that five element array to a hash (and actually, just because it comes back from a linear regression as an array doesn't mean it has to stay that way) but I'd like to understand the syntax. Pray, give insight to this lost soul?

Replies are listed 'Best First'.
Re: accessing an anonymous array
by haukex (Bishop) on Apr 11, 2020 at 20:09 UTC
    $tables{$day}->{$state} = \[$deathsToday, $m, $double, $intercept, $corr];

    This is setting that hash value to a reference to a reference to an anonymous array, not just a typical reference to an anonymous array ("arrayref"). Is that what you want? If not, you can drop the \ and access the first element via $tables{$day}{$state}[0] and the whole array via @{$tables{$day}{$state}}, or, on Perl 5.24 or higher*, $tables{$day}{$state}->@*. Note the -> operator can be dropped between {}'s and []'s.

    Otherwise, if you want to stick with your current data structure, to access the first element you'll have to say ${$tables{$day}{$state}}->[0], or, on Perl 5.24 or higher*, $tables{$day}{$state}->$*->[0]. In other words, because you've got an extra reference in there, you need the extra dereference (${...} or ->$*) before doing the ->[0] dereference to get the array element.

    * On Perl 5.20 and 5.22, this feature can be enabled with use feature 'postderef';.

    See also perlreftut and perlref.

    Update: Minor edits and added second sentence to second paragraph.

Re: accessing an anonymous array
by leszekdubiel (Scribe) on Apr 12, 2020 at 07:50 UTC
    #!/usr/bin/perl -CSDA use utf8; use Modern::Perl; no warnings qw{uninitialized}; use Data::Dumper; my $tables = { # hash ref "{", keys are day numbers 0 => { # day0 confirmed => [1, 0.334638642561918, 2, 0.8733077785172 +49, 0.8539438, 57158699], deaths => [], }, 1 => { # day1 confirmed => [2, 0.334538642561918, 2, 0.8733077785172 +49, 0.8539438, 57158699], deaths => [], }, 2 => { confirmed => [3, 0.334638442561918, 2, 0.8733077785172 +49, 0.8539438, 57158699], deaths => [], }, }; $$tables{3}{confirmed} = [3, 0.18, 2, 0.849, 0.8, 59]; $$tables{3}{deats} = [2, 3, 4]; $$tables{4} = { confirmed => [4, 1.3, 2, 1.9, 2, 57], deaths => [], }; $$tables{1}{deats}[3] = "three"; $$tables{1}{deats}[5] = "five"; $$tables{5}{alfa} = ["a", "l", "faaaaa"]; for my $d (keys %$tables) { print "\nnext day... $d\n"; for my $t (keys %{$$tables{$d}}) { print "day is $d, table is \"$t\", content: ", (join " +, ", @{$$tables{$d}{$t}}), "\n"; } } print "Dumper is: ", Dumper($tables);

    result:

    next day... 4 day is 4, table is "confirmed", content: 4, 1.3, 2, 1.9, 2, 57 day is 4, table is "deaths", content: next day... 0 day is 0, table is "confirmed", content: 1, 0.334638642561918, 2, 0.87 +3307778517249, 0.8539438, 57158699 day is 0, table is "deaths", content: next day... 2 day is 2, table is "confirmed", content: 3, 0.334638442561918, 2, 0.87 +3307778517249, 0.8539438, 57158699 day is 2, table is "deaths", content: next day... 1 day is 1, table is "deats", content: , , , three, , five day is 1, table is "confirmed", content: 2, 0.334538642561918, 2, 0.87 +3307778517249, 0.8539438, 57158699 day is 1, table is "deaths", content: next day... 5 day is 5, table is "alfa", content: a, l, faaaaa next day... 3 day is 3, table is "deats", content: 2, 3, 4 day is 3, table is "confirmed", content: 3, 0.18, 2, 0.849, 0.8, 59 Dumper is: $VAR1 = { '4' => { 'confirmed' => [ 4, '1.3', 2, '1.9', 2, 57 ], 'deaths' => [] }, '0' => { 'confirmed' => [ 1, '0.334638642561918', 2, '0.873307778517249', '0.8539438', 57158699 ], 'deaths' => [] }, '2' => { 'confirmed' => [ 3, '0.334638442561918', 2, '0.873307778517249', '0.8539438', 57158699 ], 'deaths' => [] }, '1' => { 'deats' => [ undef, undef, undef, 'three', undef, 'five' ], 'confirmed' => [ 2, '0.334538642561918', 2, '0.873307778517249', '0.8539438', 57158699 ], 'deaths' => [] }, '5' => { 'alfa' => [ 'a', 'l', 'faaaaa' ] }, '3' => { 'deats' => [ 2, 3, 4 ], 'confirmed' => [ 3, '0.18', 2, '0.849', '0.8', 59 ] } };
Re: accessing an anonymous array
by bart (Canon) on Apr 13, 2020 at 10:06 UTC
    I am sorry to say that "anonymous array" is actually a misnomer. It's not an array, it's a reference to an array.

    These two snippets are actually equivalent:

    my $r = ['a', 'b', 'c'];
    and
    my $r; { my @a = ('a', 'b', 'c'); $r = \@a; }

    So when you try using \[1] to get a reference to the anonymous array, you're ending up with a reference to an array reference.

    Drop the backslashes n front of the open square brackets: you don't need them. And start to work it out from there.

      I am sorry to say that "anonymous array" is actually a misnomer. It's not an array, it's a reference to an array.

      Well, if we're going to be nitpicky, then that is not quite correct, and your examples aren't entirely equivalent. Although both $rs are references to arrays, in my @a = ('a', 'b', 'c'); $r = \@a;, it's not an anonymous array, since it has a name, @a (even if its scope is limited). In my $r = ['a', 'b', 'c'];, the array being referenced by $r is anonymous, as it never gets any name. So in the OP's example, it is in fact an anonymous array, albeit a reference to a reference to one.

        As soon as the scope is exited, the variable is gone, and the array ref is all that is left. From then on, it's an "anonymized array". The array ref is all that is left.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11115386]
Approved by Discipulus
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2021-12-07 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (33 votes). Check out past polls.

    Notices?