Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

array references

by vivapl (Acolyte)
on Apr 02, 2003 at 19:56 UTC ( [id://247579] : perlquestion . print w/replies, xml ) Need Help??

vivapl has asked for the wisdom of the Perl Monks concerning the following question:

Help please!

I have a reference to an array of references. To print contents I would do this:

for $i(0..$#{$ref}) { print $ref->[$i][0]; }
is there any way to bypass the for loop and put the contents into a regular array without a loop? Is it even possible?
eg: @newarray= ....
any help would be greatly appreciated.

Added code tags - dvergin 2003-04-02

Replies are listed 'Best First'.
Re: array references
by Elian (Parson) on Apr 02, 2003 at 20:00 UTC
    @array = @{$arrayref};
Re: array references
by pg (Canon) on Apr 02, 2003 at 20:18 UTC
    Even for the print, you really don't need the c style code, and can do this:
    #!/usr/bin/perl use strict; my $a = \(1..10); print $_, " " for (@$a);
Re: array references
by DrManhattan (Chaplain) on Apr 03, 2003 at 03:49 UTC
    I'm not sure without <code> tags, but I think what you're trying to do is this:
    for $i (0..$#{$ref}) { print $ref->[$i]->[0]; }

    That prints the first element of each array in the arrayref.

    There are several ways to go about putting the elements into a new array. Here's what I'd do:

    my @newarray = map { $_->[0] } @$ref;