Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Pushing at Array of Arrays

by Miguel (Friar)
on Feb 25, 2005 at 00:34 UTC ( [id://434326]=perlquestion: print w/replies, xml ) Need Help??

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

Esteemed Monks,

What am I missing here:

#!/usr/bin/perl -w use strict; use warnings; use diagnostics; my @data = [ '17-02-2005','18-02-2005','19-02-2005', '20-02-2005','21-02-2005','22-02-2005', '23-02-2005','24-02-2005' ]; my %labels = ( 'C' => [0,0,0,0,7,0,0,2], 'A' => [0,0,0,0,0,0,5,4], 'B' => [0,0,0,0,0,0,0,0] ); foreach my $item (sort {$a cmp $b} keys %labels) { push @{$data[$item]}, $labels{$item}; } use Data::Dumper; print Dumper @data; __OUTPUT__ $VAR1 = [ '17-02-2005', '18-02-2005', '19-02-2005', '20-02-2005', '21-02-2005', '22-02-2005', '23-02-2005', '24-02-2005', [0,0,0,0,0,0,5,4], [0,0,0,0,0,0,0,0], [0,0,0,0,7,0,0,2] ];

Altought the output is what I want, I get these errors:

Argument "A" isn't numeric in array element. Argument "B" isn't numeric in array element. Argument "C" isn't numeric in array element.

What am I doing wrong this time?

Thanks,
Miguel

UPDATE Damn it! I posted this message at the wrong place! Sorry for this.

Replies are listed 'Best First'.
Re: Pushing at Array of Arrays
by rir (Vicar) on Feb 25, 2005 at 02:24 UTC
    Your @data initially has one element, a reference to an array of date text. Is this what you want? I suspect not. I am guessing your code is only very close to what you want.

    The "errors" are warnings. "A" and such evaluate to zero in a numeric context so your labels are pushed on to the first or zero-eth element of your data array. Since people don't usually want to convert "B" to 0, the warning is issued.

    The $item index to $data in this line causes the warnings:
    push @{$data[$item]}, $labels{$item};

    Likely you want something like the below.

    I admire the steadfastness I see in your coding effort.

    Be well,
    rir

    #!/usr/bin/perl use strict; use warnings; use diagnostics; my @data = ( # note parens not square brackets '17-02-2005','18-02-2005','19-02-2005', '20-02-2005','21-02-2005','22-02-2005', '23-02-2005','24-02-2005' ); my %labels = ( 'C' => [0,0,0,0,7,0,0,2], 'A' => [0,0,0,0,0,0,5,4], 'B' => [0,0,0,0,0,0,0,0] ); foreach my $key (sort {$a cmp $b} keys %labels) { push @data, $labels{$key}; } use Data::Dumper; print Dumper @data; __END__ $VAR1 = '17-02-2005'; $VAR2 = '18-02-2005'; $VAR3 = '19-02-2005'; $VAR4 = '20-02-2005'; $VAR5 = '21-02-2005'; $VAR6 = '22-02-2005'; $VAR7 = '23-02-2005'; $VAR8 = '24-02-2005'; $VAR9 = [ 0, 0, 0, 0, 0, 0, 5, 4 ]; $VAR10 = [ 0, 0, 0, 0, 0, 0, 0, 0 ]; $VAR11 = [ 0, 0, 0, 0, 7, 0, 0, 2 ];
      You're absolutely right! Thank you.
Re: Pushing at Array of Arrays
by Enlil (Parson) on Feb 25, 2005 at 01:50 UTC
    Since you mentioned that the output is what you want what you want your code to look like is:
    #!/usr/bin/perl -w use strict; use warnings; use diagnostics; my @data = [ '17-02-2005','18-02-2005','19-02-2005', '20-02-2005','21-02-2005','22-02-2005', '23-02-2005','24-02-2005' ]; my %labels = ( 'C' => [0,0,0,0,7,0,0,2], 'A' => [0,0,0,0,0,0,5,4], 'B' => [0,0,0,0,0,0,0,0] ); foreach my $item (sort {$a cmp $b} keys %labels) { # replaced #push @{$data[$item]}, $labels{$item}; # with push @{$data[0]}, $labels{$item}; } use Data::Dumper; print Dumper @data;
    The problem is that in the push you are using $data[$item] in which case $item (being the hash keys 'A','B','C') not being a numeric are being converted to 0 (and hence pushing things onto the first array in your AoA (@data) and a warning is thrown. Which is why I replaced $item with zero in the code above.

    -enlil

Re: Pushing at Array of Arrays
by djantzen (Priest) on Feb 25, 2005 at 01:41 UTC

    $item is getting set to 'A', 'B' and 'C' because you're sorting the hashkeys. The warning indicates that letters are invalid array indexes. The real problem here however is that you're getting correct behavior in this case accidentally, perhaps perl is trying to be DWIMy. As an experiment, try declaring @data like:

    my @data = ([],[ '17-02-2005','18-02-2005','19-02-2005', '20-02-2005','21-02-2005','22-02-2005', '23-02-2005','24-02-2005' ]);
    You'll find that perl is simply pushing the arrays in your hashtable onto the first embedded array that it finds. The solution therefore is not to use the output of your sorting routine to identify the array you wish to push to.


    "The dead do not recognize context" -- Kai, Lexx
Re: Pushing at Array of Arrays
by sh1tn (Priest) on Feb 25, 2005 at 00:59 UTC
    If this helps (index in array must be a number):
    my %labels = ( '0' => [0,0,0,0,7,0,0,2], '1' => [0,0,0,0,0,0,5,4], '2' => [0,0,0,0,0,0,0,0] );


      This solves one problem and creates another, namely, the attempt to look up nonexistent array indexes (1 and 2) will result in autovivication of two sibling arrays to the one in the zero place.


      "The dead do not recognize context" -- Kai, Lexx

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-16 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found