Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean when you try to print an array or hash using Perl and you get, Array(0xd3888)?

Tags:

arrays

hash

perl

What does it mean when you try to print an array or hash and you see the following; Array(0xd3888) or HASH(0xd3978)?

EXAMPLE

CODE

my @data = (  
['1_TEST','1_T','1_TESTER'],  
['2_TEST','2_T','2_TESTER'],  
['3_TEST','3_T','3_TESTER'],  
['4_TEST','4_T','4_TESTER'],  
['5_TEST','5_T','5_TESTER'],  
['6_TEST','6_T','^_TESTER']  
);  

foreach my $line (@data) {  
   chomp($line);  
   @random = split(/\|/,$line);  
   print "".$random[0]."".$random[1]."".$random[2]."","\n";  
}  

RESULT

ARRAY(0xc1864)  
ARRAY(0xd384c)  
ARRAY(0xd3894)  
ARRAY(0xd38d0)  
ARRAY(0xd390c)  
ARRAY(0xd3948)  
like image 555
Luke Avatar asked May 18 '10 19:05

Luke


People also ask

What is the difference between an array and a hash in Perl?

Arrays are ordered, and you access an element of an array using its numerical index. Hashes are un-ordered and you access a value using a key which is a string. Each hash key is associated with a single value and the keys are all unique inside a single hash structure.

What is the use of array in Perl?

In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable.

Can a Perl hash value be an array?

You can create arrays of hashes, hashes of arrays, and any other sort of complicated data structure you can dream up. To learn more about these, look at the Perl documentation.


2 Answers

It's hard to tell whether you meant it or not, but the reason why you're getting array references is because you're not printing what you think you are.

You started out right when iterating over the 'rows' of @data with:

foreach my $line (@data) { ... }

However, the next line is a no-go. It seems that you're confusing text strings with an array structure. Yes, each row contains strings, but Perl treats @data as an array, not a string.

split is used to convert strings to arrays. It doesn't operate on arrays! Same goes for chomp (with an irrelevant exception).

What you'll want to do is replace the contents of the foreach loop with the following:

foreach my $line (@data) {

    print $line->[0].", ".$line->[1].", ".$line->[2]."\n";
}

You'll notice the -> notation, which is there for a reason. $line refers to an array. It is not an array itself. The -> arrows deference the array, allowing you access to individual elements of the array referenced by $line.

If you're not comfortable with the idea of deferencing with arrows (and most beginners usually aren't), you can create a temporary array as shown below and use that instead.

foreach my $line (@data) {

    my @random = @{ $line };
    print $random[0].", ".$random[1].", ".$random[2]."\n";
}

OUTPUT

1_TEST, 1_T, 1_TESTER
2_TEST, 2_T, 2_TESTER
3_TEST, 3_T, 3_TESTER
4_TEST, 4_T, 4_TESTER
5_TEST, 5_T, 5_TESTER
6_TEST, 6_T, ^_TESTER

A one-liner might go something like print "@$_\n" for @data; (which is a bit OTT), but if you want to just print the array to see what it looks like (say, for debugging purposes), I'd recommend using the Data::Dump module, which pretty-prints arrays and hashes for you without you having to worry about it too much.

Just put use Data::Dump 'dump'; at beginning of your script, and then dump @data;. As simple as that!

like image 196
Zaid Avatar answered Sep 29 '22 05:09

Zaid


It means you do not have an array; you have a reference to an array.

Note that an array is specified with round brackets - as a list; when you use the square bracket notation, you are creating a reference to an array.

foreach my $line (@data)
{
    my @array = @$line;
    print "$array[0] - $array[1] - $array[2]\n";
}

Illustrating the difference:

my @data = (
['1_TEST','1_T','1_TESTER'],
['2_TEST','2_T','2_TESTER'],
['3_TEST','3_T','3_TESTER'],
['4_TEST','4_T','4_TESTER'],
['5_TEST','5_T','5_TESTER'],
['6_TEST','6_T','^_TESTER']
);

# Original print loop
foreach my $line (@data)
{
    chomp($line);
    @random = split(/\|/,$line);
    print "".$random[0]."".$random[1]."".$random[2]."","\n";
}

# Revised print loop
foreach my $line (@data)
{
    my @array = @$line;
    print "$array[0] - $array[1] - $array[2]\n";
}

Output

ARRAY(0x62c0f8)
ARRAY(0x649db8)
ARRAY(0x649980)
ARRAY(0x649e48)
ARRAY(0x649ec0)
ARRAY(0x649f38)
1_TEST - 1_T - 1_TESTER
2_TEST - 2_T - 2_TESTER
3_TEST - 3_T - 3_TESTER
4_TEST - 4_T - 4_TESTER
5_TEST - 5_T - 5_TESTER
6_TEST - 6_T - ^_TESTER
like image 39
Jonathan Leffler Avatar answered Sep 29 '22 06:09

Jonathan Leffler