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

passing an array and some scalars to a subroutne

by Anonymous Monk
on Mar 06, 2009 at 13:44 UTC ( [id://748856]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I have a problem(!) but I can't understand why is it so. I have a set of variables (Which is all scalars) and an array to be passed on to a subroutine. The array and the scalars come from two different subroutines. Please see the code below

my ($id_snp, $id_assay, $session, $load_set) = @_; #these variables my (@lines) = @_; #this array my $plate_id = $cgi->param('plate'); my $barcode = Verifynumber($plate_id); my $plate = $barcode->{Number}; my $dna_well; $dna_well = $STATEMENTS->get("geno_manifest.sql_lib::get_dnawell", +[$plate, lc$lines[2]]); print "dna_well: $dna_well->[0]->[0]"; print "SNP: $id_snp, assay:$id_assay , session $session, load_set: + $load_set";
The result I get is:
SNP: 8925827, assay:67 , session 5884, load_set: 227dna_well: 5381721( + which is right. and the rest of the results are the values of the scalar are from the array @lines). SNP: True, assay:3200768 , session A1, load_set: EPS316120dna_well: 53 +81817SNP: True, assay:3200768 , session A2, load_set: EPS31807
Also, the dna_well which is processed in this subroutine hasn't got a problem. Could any one suggest me what I am doing wrong here. And why is that using the array values if used along with the variables. Thanks for you help in advance
The @lines is something like this:
True 3200768 A1 EPS316120 8.535 17.575 GG 0.90 True 3200768 A2 EPS318077 8.820 17.126 GG 0.95 True 255 A3 EPS316121 17.084 13.650 GA 0.97 True 3200768 A4 EPS318078 8.541 16.653 GG 0.94 True 16744448 A5 EPS316122 18.267 3.880 AA + 1.00 True 255 A6 EPS318079 13.130 11.004 GA 0.91
Thanks a lot.

Replies are listed 'Best First'.
Re: passing an array and some scalars to a subroutne
by bellaire (Hermit) on Mar 06, 2009 at 13:52 UTC
    First off, I have pretty much no idea what you were trying to say, your description is incomprehensible. However, just looking at your code:
    my ($id_snp, $id_assay, $session, $load_set) = @_; #these variables my (@lines) = @_; #this array
    The value of @_ doesn't change between these two lines. That means that the first four values of @lines are going to be the same as the four variables you grab on the first line. Is that what you are trying to do, or is the value of @lines supposed to be the arguments after those four?
      Hi,
      Sorry I dint mention that they both are different. @lines is a slurp of a tab delimited file where as the $id_snp, $id_assay, $session, $load_set are values from the database. I need all these values to be loaded into another table in this subroutine. Thanks a lot.
Re: passing an array and some scalars to a subroutne
by toolic (Bishop) on Mar 06, 2009 at 14:17 UTC
    Maybe you want?
    my ($id_snp, $id_assay, $session, $load_set, @lines) = @_;
Re: passing an array and some scalars to a subroutne
by olus (Curate) on Mar 06, 2009 at 14:22 UTC

    You can also shift the args.

    Also, since the arguments are passed as a list, you could start considering passing references to the arrays and deref them in the sub. That way you can pass other args after the first array, or multiple arrays (that would otherwise be considered a single one).

    use strict; use warnings; my $arg1 = "abc"; my @arg2 = (1, 2, 3, 4); show_args($arg1, \@arg2); sub show_args { my $arg1 = shift; my $arg2 = shift; my @lines = @{$arg2}; print @lines; }
Re: passing an array and some scalars to a subroutne
by Bloodnok (Vicar) on Mar 06, 2009 at 14:11 UTC
    As bellaire pointed out, you load the first 4 args in both cases, I'm guessing what you meant was:
    . my ($id_snp, $id_assay, $session, $load_set) = splice @_, 0, 4; #thes +e variables my @lines = @_; #this array . .
    Update: Mis-spelt bellaires monika

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-20 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found