How do I get the data out of this array?

  • Thread starter gimme_this_gimme_that
  • Start date
G

gimme_this_gimme_that

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.
 
D

Dave Weaver

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]
 
P

Paul Lalli

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top