Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: element of an array of arrays

by frozenwithjoy (Curate)
on Jun 17, 2013 at 02:14 UTC ( #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


Comment on Re: element of an array of arrays
Select or Download Code
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
Node Status?
node history
Node Type: note [id://1039252]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2015-07-31 05:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls