Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: element of an array of arrays

by frozenwithjoy (Priest)
on Jun 17, 2013 at 02:14 UTC ( [id://1039252]=note: print w/replies, xml ) Need Help??


in reply to element of an array of arrays

You don't actually have an array of arrays:

use Data::Printer; p @Records; p @RecCheck;
[ [0] "AAA,20130610,730,1015, ", [1] "BBB,20130610,1015,1200, ", [2] "CCC,20130610,1230,1400, ", [3] "DDD,20130610,1415,1530, " ] [ [0] "AAA,20130610,730,1015, ", [1] "BBB,20130610,1015,1200, ", [2] "CCC,20130610,1230,1400, ", [3] "DDD,20130610,1415,1530, " ]

I left your @Records almost as is (only removed new line) and changed @RecCheck so that it would be an array of arrays like I think you wanted:

#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use Data::Printer; my @Records; push( @Records, join ',', ( "AAA", 20130610, 730, 1015 ) ); push( @Records, join ',', ( "BBB", 20130610, 1015, 1200 ) ); push( @Records, join ',', ( "CCC", 20130610, 1230, 1400 ) ); push( @Records, join ',', ( "DDD", 20130610, 1415, 1530 ) ); my @RecordRowFields; my @RecCheck; for my $RecordRow (@Records) { @RecordRowFields = split /,/, $RecordRow; push( @RecCheck, [@RecordRowFields] ); } say "Single element: $RecCheck[1][2]"; p @RecCheck; __END__ [ [0] [ [0] "AAA", [1] 20130610, [2] 730, [3] 1015 ], [1] [ [0] "BBB", [1] 20130610, [2] 1015, [3] 1200 ], [2] [ [0] "CCC", [1] 20130610, [2] 1230, [3] 1400 ], [3] [ [0] "DDD", [1] 20130610, [2] 1415, [3] 1530 ] ] Single element: 1015

Replies are listed 'Best First'.
Re^2: element of an array of arrays
by JockoHelios (Scribe) on Jun 17, 2013 at 02:21 UTC
    So when I push arrays onto an empty array, that doesn't create an array of arrays ?

    Interesting, that's not where I thought the problem was. Now I'm at a loss. How would I create an array of arrays with that data ?

    Dyslexics Untie !!!
      I just updated my answer w/ code that creates an AoA. You were basically pushing a list into an array and it gets incorporated into the array just like all the elements. What you needed to do was push an array reference.

      So when I push arrays onto an empty array, that doesn't create an array of arrays ?

      an array of arrays is an array of arrayrefs

      When you push @arra, @array you're pushing a list onto @arra

      push @arra, \@array; ## pushing arrayreference, array of arrays achieved

        perldoc -f push
        push ARRAY,LIST
        push EXPR,LIST
        Treats ARRAY as a stack by appending the values of LIST to the end of ARRAY. The length of ARRAY increases by the length of LIST.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found