0

I am having trouble in parsing an array. I have used

print Dumper($variable)

to get

$VAR1 = [
          'joshn',
          'taylor'
        ];

I need to get the individual elements josh and taylor. How can I obtain it?

2 Answers 2

2
# get last element 
my $last = $variable->[-1];

# get first element
my $first = $variable->[0]; # cryptic equivalent: $$variable[0] (don't use it) 
# get second element
my $second = $variable->[1]; # also $$variable[1]

# same effect as above
my ($first, $second) = @{$variable}; # or @$variable for short
Sign up to request clarification or add additional context in comments.

2 Comments

@Kenosis yes, but there are reasons to dislike them
I prefer the dereferencing arrow you've used, but thought I'd include the above for the OP.
0

I guess $variable is a ref to an array, then

for(@{$variable}) {
    print $_, "\n"
}

EDIT: To access last element in an array:

my @array = @{$variable};
print $array[$#array];

2 Comments

How can I directly access the last element rather than using for loop
I'll answer that even if it has nothing to do with your original question. This is not a forum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.