Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do I access an array for which I only have a reference?

by Anonymous Monk
on Mar 31, 2000 at 17:41 UTC ( [id://6594]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (references)

How do I access an array for which I only have a reference?

Originally posted as a Categorized Question.

  • Comment on How do I access an array for which I only have a reference?

Replies are listed 'Best First'.
Re: How do I access an array for which I only have a reference?
by chromatic (Archbishop) on Mar 31, 2000 at 21:39 UTC

    You dereference the array reference.

    For example, given

    my @array = ( 'one', 'two', 'buckle my shoe', ); my $array_ref = \@array;
    then you can access the array to which $array_ref refers by prefixing a @ to it, e.g.:
    for ( @$array_ref ) { ... }
    The @ symbol is essentially an array dereference operator. It can dereference any value which is an array reference, whether that value is in a variable (e.g. $array_ref) or is the result of some more complex expression. The general syntax is @{ ... }; but you can omit the curlies when the expression is a simple scalar variable holding an array ref.

    To access elements of an array to which you have a reference, insert an arrow between the array ref and the square brackets used for indexing, e.g.

    print "The third element is $array_ref->[2] \n";

    Reference types are strictly checked by the interpreter; you can't dereference an array ref as a hash, for example.

    print keys %{ $array_ref }; # fatal!
    The error message I get is "Can't coerce array into hash at - line ..."

Re: How do I access an array for which I only have a reference?
by little_mistress (Monk) on Apr 01, 2000 at 02:22 UTC

    A really good book to get is O'Reilly's Advanced Perl Programming if you are going to get into this sort of thing. But here is my brief explanation.

    Let's say you have an array reference...

    my $r_array = [ 'inky', 'blinky', 'blu' ];
    Now the question is, what exactly is in $r_array? It's sort of similar to the concept of pointers in C: a reference contains the address of where the data is actually located in memory. If you print $r_array directly, you get something funky looking like ARRAY(0xc70858), which is clearly not the data stored in the array. So think about your reference this way: it's just a sign saying where the data really is.

    To get at the data where it really is, you have to dereference the reference, using the following syntax:

    @{ $r_array }
    You could use it in code, like this:
    for ( @{ $r_array } ) { print "$_\n"; }
    Now, as a shorthand, Perl lets you omit the curly braces (for reference variables only):
    for ( @$r_array )

Re: How do I access an array for which I only have a reference?
by le (Friar) on Jun 07, 2000 at 00:59 UTC

    In places where you want the index of the last element (e.g. in foreach loops), you'd use the syntax

    $#{ $arrayref }
    It's just like the usual $#array, but with an array reference value in place of the array variable name. And you need the curly braces.

    So, to iterate over the elements of an array to which you have a reference, using an index counter:

    for ( my $i = 0; $i <= $#{ $arrayref }; $i++ ) { print "$arrayref->[$i] \n"; }

    I think this is useful when you have arrays of arrays, or even more complex data structures.

Re: How do I loop over a reference to an array?
by bageler (Hermit) on Mar 08, 2004 at 23:18 UTC
    You can also use map as the looping mechanism...some may call it map abuse ;) Dereference in the usual fashion.
    $arrayref = [0..10]; map {print "$_\n"} @$arrayref;
      Map Abuse Map Abuse!! :)

      IMHO,a map in void context is a sign you should be using a for loop instead.

      print qq{$_\n} for ( @$arrayref );
      Update: Oops, read rest of thread. Apologize for stating the obvious. :P

      Wonko.

Re: How do I loop over a reference to an array?
by sdaniels (Initiate) on Feb 02, 2001 at 01:47 UTC
    Or you could try:
    foreach my $i ( 0 .. $#$array ) { # dereference the element print $array->[$i], "\n"; }

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://6594]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found