sbnation
sbnation

Reputation: 149

Printing a two-dimensional array

I've tried searching for this, but I'm not sure what this condition is called.

my @tgs = (
    ['article series', 'sed & awk', 'troubleshooting', 'vim', 'bash'],
    ['ebooks', 'linux 101', 'vim 101', 'nagios core', 'bash 101' ]
);

print $_ foreach @tgs;

results in:

ARRAY(0x1fedcb8)ARRAY(0x200fe80)

What does this mean? Why is this happening, and to what does 0x1fedcb8 and 0x200fe80 refer? I understand this is most likely a commonly asked question, but please bear with me.

Upvotes: 0

Views: 122

Answers (1)

ikegami
ikegami

Reputation: 386646

You have an array that contains two references to other arrays. You are getting garbage because you are printing the references rather than the content of the arrays referenced by those references. (The hex numbers are the memory addresses at which the referenced arrays are located.)

You can print out this "two-dimensional" array using

for my $row (@tgs) {
   print(join(' ', @$row), "\n");
}

Docs: perldsc, perllol

Upvotes: 1

Related Questions