Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

passing array to a sub

by pitbull3000 (Beadle)
on Aug 25, 2001 at 15:18 UTC ( #107809=perlquestion: print w/replies, xml ) Need Help??

pitbull3000 has asked for the wisdom of the Perl Monks concerning the following question:

i want to pass some scalars and an array to a sub, but how can get the values then
&example($scalar1, $scalar2, @info1); sub example { $scalar1 = shift;$scalar2 = shift;@info1 = @_; ..... }
but unfortunately this isnt working...

Replies are listed 'Best First'.
Re: passing array to a sub
by Masem (Monsignor) on Aug 25, 2001 at 15:32 UTC
    This ought to work, so there might be something wrong with the code before the call to your example method, or within the example method after you have read in values. Make sure that $scalar1, $scalar2, and @info1 are containing what you expect before you go into the subroutine call.

    If for some reason everything's fine, there's a couple other ways to try:

    sub example { my ( $scalar1, $scalar2, @info1 ) = @_; ... }
    or using references:
    &example( $scalar1, $scalar2, \@info1 ) = @_; ... sub example { my $scalar1 = shift; my $scalar2 = shift; my $arrref = shift; my @info1 = @$arrref; ... }

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

Re: passing array to a sub
by George_Sherston (Vicar) on Aug 25, 2001 at 15:42 UTC
    I don't think the problem can be in how you pass the values, because this works:
    $s1 = "first scalar"; $s2 = "second scalar"; @a1 = ("first item","second item","third item","fourth item"); &example($s1, $s2, @a1); sub example { $es1 = shift;$es2 = shift;@ea1 = @_; print "S1: $es1\n"; print "S2: $es2\n"; print "ARRAY:\n"; for $i (@ea1) {print "$i ; "} } #prints the following: #S1: first scalar #S2: second scalar #ARRAY: #first item ; second item ; third item ; fourth item ;
    So the problem must be somewhere else - perhaps in what you are doing with the sub, or how you define the variables before passing them. More detail?

    George Sherston
      shame on my empty head, the problem was as you both stated, in the code before passing the variables. the array was empty, so i couldnt get any value
Re: passing array to a sub
by Zaxo (Archbishop) on Aug 25, 2001 at 15:55 UTC

    Setting values to feed example() and printing inside the function works. You are courting trouble by reusing $scalar1 and friends as names in the sub and not making them lexical with my.

    Please say what the symptoms of 'not working' are, and show how the variables are declared and assigned.

    $ cat pit.pl #!/usr/bin/perl -- use strict; use warnings; my $scalar1 = 'foo'; my $scalar2 = 'ferall'; my @info1 = ( 0..10 ); &example($scalar1, $scalar2, @info1); sub example { $scalar1 = shift;$scalar2 = shift;@info1 = @_; # bad # prefer lexical scope, ought not to reuse names # my ($scalar1, $scalar2, @info1) = @_; print $scalar1,$/; print $scalar2,$/; print @info1,$/; # ..... } $ ./pit.pl foo ferall 012345678910 $

    After Compline,
    Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://107809]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2022-12-07 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?