?
Solved

dereferencing an arrayref returned from a subroutine

Posted on 2005-04-23
4
Medium Priority
?
222 Views
Last Modified: 2010-03-05
I have a sub that returns an array reference like so:

sub mysub($@) {
   #I'll do stuff later, but right now we'll just pass the array back
   return \@_;
   }


I assign it to a scalar like so:
my @array = ("one", "two", "three", "four", "five");
my $arrayref = mysub(@array);

I'm getting stuck trying to figure out how to access those array elements using the arrayref.
I have:

foreach my $i (@$arrayref) {
       # I want to print it here.
        print("@{$arrayref}[$i] \n");   # generates "use of unintialized value" error
        print("${$arrayref}[$i]  \n ");  # generates "use of uninitialzed value" error
        print("{@$arrayref}[$i]\n ");   #  prints {5}[5] , sees $arrayref as a scalar, not an array
        print("@{$arrayref}->[$i]\n");   #generates "using an array as a reference is deprecat                                                                                                                                                                                                
                                                               #also generates "use of unintialized value" error
}


What I'd like to see is
one
two
three
four
five

Any ideas? I just can't get this syntax down.

ps:
use strict.

Thanks!
0
Comment
Question by:detender
  • 2
4 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 13852631
sub mysub(@) {
   #I'll do stuff later, but right now we'll just pass the array back
   return \@_;
   }

my @array = ("one", "two", "three", "four", "five");
my $arrayref = mysub(@array);


foreach my $i (@$arrayref) {
       # I want to print it here.
        print("$i\n");
}
0
 

Author Comment

by:detender
ID: 13855141
Thanks ozo, that's great. Much simpler than what I was going for.

I suppose I should have been more clear, but I will need to access these array elements individually. I'm splitting a comma-sep file, sending a line at a time to a sub to process it, and bringing it back for insertion into a db, so I won't be using a foreach loop in the final script. It was just a simple construct for me to test with.

How would I access the array elements from the arrayref? That's what I really want to know. Do I use array subscript or what?

Thanks
0
 
LVL 18

Expert Comment

by:kandura
ID: 13855160
for( my $i = 0; $i < scalar(@$arrayref); $i++ ) {   # or $i <= $#$arrayref
    print $arrayref->[$i],$/;
}
0
 
LVL 85

Accepted Solution

by:
ozo earned 1000 total points
ID: 13855202
foreach my $i (0..$#$arrayref) {
       # I want to print it here.
        print("$arrayref->[$i]\n");
        print("${$arrayref}[$i]\n");
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question