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

Hi all, For some reason this does not work at all, i know that my @value contains data!
while (<IN2>) { chomp $_; $i++; ($pdb_res) = (split /\s+/,$_) [3]; #print "$pdb_res\n"; if ($pdb_res == $value[$i]) {s/100.0/3.0/}; print OUT "$_\n"; }

Replies are listed 'Best First'.
Re: going through an array
by jeffa (Bishop) on Nov 21, 2004 at 00:26 UTC

    You need tell us what you are trying to do instead of just posting your code. If you can't explain what you are trying to do, then you shouldn't expect to be able to write successful code. You also need to show exactly what @value contains, and a few lines from the file you opened.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      so initially read the first colum from here into @value
      112 104.35 141.94 115 22.34 29.13 while (<IN>) { ($rub,$res) = split (/\s+/,$_); push @value, "$res\n"; }
      where this first column value agrees with the third colum in the next file, replace 100.0 with 3.0
      1 59 49 0.0 0 100.0 0.0 1 60 50 0.0 0 100.0 0.0 using code supplied in first post
        Your description of column counting seems to be a bit off.

        In your case, the SECOND column values (104.35, 22.34) get populated into @value. If you had wanted the first column, you would need to "push @value,$rub" . (And why are you adding the trailing "\n" ?)

        Also, in your original post, taking the index [3] from the result of the "split" gets you the FOURTH column (zero based).

        Most likely, a better understanding of perl's zero-based indexing would help fix this program.

            Earth first! (We'll rob the other planets later)

        btw these are only segments from complete files
Re: going through an array
by steves (Curate) on Nov 21, 2004 at 00:25 UTC

    Maybe you could show us the code that puts data in @value. If the data is not numeric your == comparison could be the problem -- try eq instead.

Re: going through an array
by Zaxo (Archbishop) on Nov 21, 2004 at 07:37 UTC

    I have no idea if this will fix your problem, but your substitution probably needs to have the dot backwhacked,     if ($pdb_res == $value[$i]) {s/100\.0/3.0/}; Otherwise it matches any character after '100' and before a zero.

    Also, ($pdb_res) = (split /\s+/,$_) [3]; is written as well by $pdb_res = (split)[3]; but that does not reflect any error and does not change the correctness of the code.

    After Compline,
    Zaxo

      thanks, it seems that the problem was with the trailing \n added to the @value