Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get the first item of the list returned by function

A simple simulation of the problem:

use strict;
use warnings;

sub uniq
{
  my %seen;
  grep !$seen{$_}++, @_;
}

my @a = (1, 2, 3, 1, 2);

print shift @{uniq(@a)}; 

Can’t use string ("3") as an ARRAY ref while "strict refs" in use

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Need to impose a list context on the function call, and then pick the first element from the list.

The print, or any other subroutine call, already supplies a list context. Then one way to extract an element from a list

print +( func(@ary) )[0];

This disregards the rest of the list.

That + is necessary (try without it), unless we use another set of parens, that is

print( (func(@ary))[0] );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading