How do I get the data out of this array?

Discussion in 'Perl Misc' started by gimme_this_gimme_that@yahoo.com, Jun 5, 2007.

  1. Guest

    In the Perl Debugger a variable $vc looks like this:

    DB<1> p $vc
    ARRAY(0x19d1f9c)
    DB<2> x $vc
    0 ARRAY(0x19d1f9c)
    0 ARRAY(0x19e0694)
    0 2.0
    1 3.0
    1 ARRAY(0x19e064c)
    0 7.0
    1 6.0
    2 ARRAY(0x19e067c)
    0 8.0
    1 12.0

    How to I get :
    2 3
    7 6
    8 12

    Thanks.
     
    , Jun 5, 2007
    #1
    1. Advertising

  2. Dave Weaver Guest

    <> wrote:
    > In the Perl Debugger a variable $vc looks like this:
    >
    > DB<1> p $vc
    > ARRAY(0x19d1f9c)
    > DB<2> x $vc
    > 0 ARRAY(0x19d1f9c)
    > 0 ARRAY(0x19e0694)
    > 0 2.0
    > 1 3.0
    > 1 ARRAY(0x19e064c)
    > 0 7.0
    > 1 6.0
    > 2 ARRAY(0x19e067c)
    > 0 8.0
    > 1 12.0
    >
    > How to I get :
    > 2 3
    > 7 6
    > 8 12


    At a command prompt run
    perldoc perlref
    and learn all about references. That should give you the knowledge you
    need to understand your data structure above.

    To give you a clue:
    print $vc->[0][0]
     
    Dave Weaver, Jun 5, 2007
    #2
    1. Advertising

  3. Paul Lalli Guest

    On Jun 4, 11:11 pm, ""
    <> wrote:
    > In the Perl Debugger a variable $vc looks like this:
    >
    > DB<1> p $vc
    > ARRAY(0x19d1f9c)
    > DB<2> x $vc
    > 0 ARRAY(0x19d1f9c)
    > 0 ARRAY(0x19e0694)
    > 0 2.0
    > 1 3.0
    > 1 ARRAY(0x19e064c)
    > 0 7.0
    > 1 6.0
    > 2 ARRAY(0x19e067c)
    > 0 8.0
    > 1 12.0


    This tells you that $vc is a reference to an array. The array that
    $vc references contains three elements. Each of those elements are
    also references to arrays. The arrays that *those* references
    reference contain two elements each, all numbers.

    Please see:
    perldoc perlreftut

    > How to I get :
    > 2 3
    > 7 6
    > 8 12


    Step by step:
    $vc is a reference to an array
    @{$vc} is the array that $vc references
    ${$vc}[0] is the first element of @{$vc}
    The arrow rule lets us write that as $vc->[0]
    $vc->[0] is also a reference to an array.
    @{$vc->[0]} is the array that $vc->[0] references. This array
    contains (2, 3);

    So you could do a loop like the following:
    foreach my $ref (@{$vc}) {
    #here, $ref is one of the "inner" references
    foreach my $elem (@{$ref}) {
    print "$elem ";
    }
    print "\n";
    }

    Once you understand the above, you can make it much more concise:

    foreach my $ref (@$vc) {
    print "@{$ref}\n";
    }

    Hope this helps,
    Paul Lalli

    P.S. The final example presumes you've not mucked with the $"
    variable.
     
    Paul Lalli, Jun 5, 2007
    #3
  4. Guest

    Thanks Paul.

    You're a pro.
     
    , Jun 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2FtZWVy?=
    Replies:
    1
    Views:
    937
    Bruce Barker
    Oct 4, 2005
  2. Helmut
    Replies:
    0
    Views:
    656
    Helmut
    Oct 2, 2003
  3. Guest
    Replies:
    0
    Views:
    428
    Guest
    Oct 29, 2008
  4. GRnewprog

    Trying to get the Array out of this function

    GRnewprog, Dec 19, 2010, in forum: C Programming
    Replies:
    0
    Views:
    245
    GRnewprog
    Dec 19, 2010
  5. Dave

    Using Datarow, how do I get the data out of each

    Dave, Feb 12, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    279
    Dave Tessitore
    Feb 13, 2004
Loading...

Share This Page