Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Array lookup

by 1nickt (Canon)
on Apr 17, 2018 at 00:48 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1213032]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found