Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Array lookup

by 1nickt (Abbot)
on Apr 17, 2018 at 00:48 UTC ( #1213032=note: print w/replies, xml ) Need Help??


in reply to Array lookup

Another problem you have is in how you are passing your arguments to your subroutine.

$ perl -Mstrict -wE 'sub say_args { my @foo = shift; my @bar = shift; +say "foo: $_" for @foo; say "bar: $_" for @bar; } my @foo = qw/a b c/ +; my @bar = qw/x y z/; say_args( @foo, @bar );' foo: a bar: b $ perl -Mstrict -wE 'sub say_args { my @foo = shift; my @bar = shift; +say "foo: $_" for @foo; say "bar: $_" for @bar; } my @foo = qw/a b c/ +; my @bar = qw/x y z/; say_args( \@foo, \@bar );' foo: ARRAY(0x17b85e8) bar: ARRAY(0x17b9110) $ perl -Mstrict -wE 'sub say_args { my $foo = shift; my $bar = shift; +say "foo: $_" for @$foo; say "bar: $_" for @$bar; } my @foo = qw/a b +c/; my @bar = qw/x y z/; say_args( \@foo, \@bar );' foo: a foo: b foo: c bar: x bar: y bar: z
As you can see you need to pass references to your arrays (else they will be concatenated, and additionally you will only get one element at a time with shift), and then dereference them inside the subroutine. See perlreftut.

Hope this helps!


The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1213032]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2020-10-25 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?