Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Assigning Variables to String Elements

by ig (Vicar)
on Dec 07, 2013 at 00:47 UTC ( [id://1066088]=note: print w/replies, xml ) Need Help??


in reply to Assigning Variables to String Elements

length probably isn't doing what you think it does. Try: for($k = 0; $k < @PAR1; $k++).

Or, even better:

for my @PAR1_info (@PAR1) { $pi = split('\t', $PAR1_info[0]); $pi_sum = $pi_sum + $pi; $L = split('\t', $PAR1_info[1]); $L_sum = $L_sum + $L; $differences = split('\t', $PAR1_info[2]); $differences_sum = $differences_sum + $differences; $coverage = split('\t', $PAR1_info[3]); $coverage_sum = $coverage_sum + $coverage; my $PAR1_diversity = (($pi_sum/$L_sum)/($differences_sum/$cove +rage_sum)); }

Edit: As AnomalousMonk pointed out, the above code doesn't compile. That's what I get for neither thinking nor testing. What I thought I was thinking was more like the following, which does compile (after a bit of testing, fixing bugs and making presumptive changes):

$pi_sum = 0; $L_sum = 0; $differences_sum = 0; $coverage_sum = 0; for my $PAR1 (@PAR1) { my @PAR1_info = split(/\t/, $PAR1); $pi = $PAR1_info[0]; $pi_sum = $pi_sum + $pi; $L = $PAR1_info[1]; $L_sum = $L_sum + $L; $differences = $PAR1_info[2]; $differences_sum = $differences_sum + $differences; $coverage = $PAR1_info[3]; $coverage_sum = $coverage_sum + $coverage; my $PAR1_diversity = (($pi_sum/$L_sum)/($differences_sum/$c +overage_sum)); }

Which could be reduced to:

$pi_sum = 0; $L_sum = 0; $differences_sum = 0; $coverage_sum = 0; for my $PAR1 (@PAR1) { my ($pi, $L, $differences, $coverage) = split(/\t/, $PAR1); $pi_sum = $pi_sum + $pi; $L_sum = $L_sum + $L; $differences_sum = $differences_sum + $differences; $coverage_sum = $coverage_sum + $coverage; my $PAR1_diversity = (($pi_sum/$L_sum)/($differences_sum/$c +overage_sum)); }

And, prior to this, chomping the input is probably appropriate:

my @X_info = <CHR_X_INPUT>; chomp(@X_info);

Replies are listed 'Best First'.
Re^2: Assigning Variables to String Elements
by AnomalousMonk (Archbishop) on Dec 07, 2013 at 15:13 UTC
    for my @PAR1_info (@PAR1) {
        ...
    }

    This does not compile:  Missing $ on loop variable at ...

Re^2: Assigning Variables to String Elements
by ccelt09 (Sexton) on Dec 07, 2013 at 01:41 UTC

    thank you, length most certainly was not doing what I thought it was doing! Now I can only imagine I am not splitting the  @PAR1_info array properly.

      $pi = split('\t', $PAR1_info[0]);

      Split returns an array. When you assign an array to a scalar, the array is evaluated in a scalar context and an array evaluated in a scalar context gives the number of elements in the array, not the first element of the array.

      There are, as usual with Perl, several ways to get the first element. Here are a couple:

      my ($pi) = split(/\t/, $PAR1_info[0]);

      my $pi = (split(/\t/, $PAR1_info[0])[0];

      Note also that split takes a regular expression (pattern) as its first argument, not a string.

      Edit: Some days I should stay away from my keyboard....

      As dave_the_m points out, what I said about split returning an array is incorrect. In fact (as is generally the case with functions) what split does depends on the context in which it is evaluated. Split does various things differently when evaluated in scalar context rather than list or void context. Of relevance here, from split:

      Splits the string EXPR into a list of strings and returns the list in list context, or the size of the list in scalar context.

      And, while split is documented to take a pattern, that pattern can be a string.

        Split returns an array.
        Strictly speaking, no it doesn't. It returns a list, and in scalar context it is designed to return a list of one element, that element being the number of items that split would have returned in list context. This semantic distinction doesn't make any practical difference for split, but would for something like splice.

        Dave.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found