detender
asked on
dereferencing an arrayref returned from a subroutine
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!
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]\
#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!
ASKER
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
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
for( my $i = 0; $i < scalar(@$arrayref); $i++ ) { # or $i <= $#$arrayref
print $arrayref->[$i],$/;
}
print $arrayref->[$i],$/;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#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");
}