Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Passing Lists to Functions?

by Anonymous Monk
on May 16, 2008 at 22:43 UTC ( [id://687042]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I am trying to pass a two dimensional array (or a list of references you may say) along with some numbers into a function in the following way:
@reduced_protein = shift (\@reduced_protein,($x2-$x1),($y2-$y1),($z2-$ +z1)); # reduced protein is 2D array with each line having some properties ab +out the protein. The shift function just shifts the coordinates of th +e all lines thus the protein (given below) sub shift { my ($reduced_protein, $shift_x, $shift_y, $shift_z); my @reduced_protein=@$reduced_protein; my $index1; my $protein_length=findColumnLength(@reduced_protein); for ... (shifting operation) return @reduced_protein; }
This gives the error "Type of arg 1 to shift must be array (not list)..." how do we pass such a list to a function? Thanks for the help

Replies are listed 'Best First'.
Re: Passing Lists to Functions?
by RMGir (Prior) on May 16, 2008 at 22:48 UTC
    shift is a built-in function :)

    Try calling your function shiftProtein, and see what happens...


    Mike
Re: Passing Lists to Functions?
by johngg (Canon) on May 16, 2008 at 22:57 UTC
    Also note that arguments are passed into subroutines in the array @_ so you need to populate your variables from that. It might also be an idea to change the name of the array reference to avoid confusion.

    sub shiftProtein { my ( $ref_to_reduced_protein, $shift_x, $shift_y, $shift_z ) = @_; my @reduced_protein = @$ref_to_reduced_protein; ...

    I hope this is of use.

    Cheers,

    JohnGG

Re: Passing Lists to Functions?
by pc88mxer (Vicar) on May 16, 2008 at 22:49 UTC
    You should avoid using shift as the name of a subroutine. In this call:
    @reduced_protein = shift (\@reduced_protein,($x2-$x1),($y2-$y1),($z2-$ +z1));
    perl thinks you want the built-in shift function which takes an array (not an array ref) as the first argument.

    Just rename your subroutine to something else, or qualify it with :::

    some_package::shift(\@reduced_protein, ...);
Re: Passing Lists to Functions?
by mwah (Hermit) on May 17, 2008 at 12:14 UTC

    The most significant error (shift as a sub) has been already mentioned. (Put a large 'S' as the first char if you need to name it that way).

    I'd like to make a comment to other aspects of your code. If you give a reference to something into a sub, this (referenced) object will be modified, so you won't need to copy it back and forth.

    ... # the array will be modified 'in place', so theres # no necessity to copy stuff around Shift( \@reduced_protein, $x2-$x1, $y2-$y1, $z2-$z1 ); ... sub Shift { my ($rp_ref, $x, $y, $z) = @_; # traditional way to feed a sub my $index1; # the expression @$array_ref expands a reference to a list my $protein_length = findColumnLength(@$rp_ref); for (shifting operation) { ... # instead of: $reduced_protein[i], # you say: $rp_ref->[i] ... } # no need to return anything, the original array is # already modified }

    Regards

    mwa

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found