ChristopherDBerry
ChristopherDBerry

Reputation: 1772

Perl dereferencing array for specific value

So, in Perl, I have an array within an object (so, a reference to an array), and I want to find the first value of that array.

I find myself using code like the following quite often:

my $server_ref = $self->{source_env}->{server};
my @servers = @$server_ref;
my $main_server = $servers[0];

That works, but I'm sure I could do this without all the intermediate lines and variables.

Can someone help me with the syntax?

Upvotes: 2

Views: 92

Answers (2)

Patrick B.
Patrick B.

Reputation: 12353

Try $server_ref->[0], it should work.

Upvotes: 0

Gabriel Ross
Gabriel Ross

Reputation: 5198

Try:

my $main_server = $self->{source_env}->{server}->[0];

Upvotes: 6

Related Questions