lundi 29 mars 2021

Why is Perl giving "Can't modify string in scalar output" error?

I'm pretty new to Perl and this is my most complex project yet. Apologies if any parts of my explanation don't make sense or I miss something out - I'll be happy to provide further clarification. It's only one line of code that's causing me an issue.

The Aim:

I have a text file that contains a single column of data. It reads like this:

0
a,a,b,a
b,b,b,a
1
a,b,b,a
b,b,b,a

It continues like this with a number in ascending order up to 15, and the following two lines after each number are a combination of four a's or b's separated by commas. I have tied this file to an array @diplo so I can specify specific lines of it.

I also have got a file that contains two columns of data with headers that I have converted into a hash of arrays (with each of the two columns being an array). The name of the hash is $lookup and the array names are the names of the headings. The actual arrays only start from the first value in each column that isn't a heading. This file looks like this:

haplo           frequency
"|5,a,b,a,a|"   0.202493719
"|2,b,b,b,a|"   0.161139191
"|3,b,b,b,a|"   0.132602458

This file contains all of the possible combinations of a or b at the four positions combined with all numbers 0-14 and their associated frequencies. In other words, it includes all possible combinations from "|0,a,a,a,a|" followed be "|1,a,a,a,a|" through to "|13,b,b,b,b|" and "|14,b,b,b,b|".

I want my Perl code to go through each of the combinations of letters in @diplo starting with a,a,b,a and record the frequency associated with the row of the haplo array containing each number from 0-14, e.g. first recording the frequency associated with "|0,a,a,b,a|" then "|1,a,a,b,a|" etc.

The output would hopefully look like this:

0   #this is the number in the @diplo file and they increase in order from 0 up to 15
0.011     0.0023    0.003    0.0532    0.163    0.3421    0.128    0.0972    0.0869    0.05514    0.0219    0.0172    0.00824    0.00886    0.00196 #these are the frequencies associated with x,a,a,b,a where x is any number from 0 to 14.

My code: And here is the Perl code I created to hopefully sort this out (there is more to create the arrays and such which I can post if required, but I didn't want to post a load of code if it isn't necessary):

my $irow=1;   #this is the row/element number in @diplo
my $lrow=0;      #this is the row/element in $lookup{'haplo'}
my $copynumber=0;
#print "$copynumber,$diplo[2]";
while ($irow<$diplolines-1) {
while ($copynumber<15) {
while ($lrow<$uplines-1) {
if ("|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]) {  ##this is the only line that causes errors
if ($copynumber==0) {
print "$diplo[$irow-1]\n"; 
#print "$lookup{'frequency'}[$lrow]\t";
}
print "$lookup{'frequency'}[$lrow]\t"; 
}
$lrow=$lrow+1;
}
$lrow=0;
$copynumber=$copynumber+1;
}
$lrow=0;
$copynumber=0;
$irow=$irow+1;
}

However, the line if ("|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]) is causing an error Can't modify string in scalar assignment near "]) ". I have tried adding in speech marks, rounded brackets and apostrophes around various elements in this line but I still get some sort of variant on this error. I'm not sure how to get around this error.

Apologies for the long question, any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire