0

I created a hash where the keys are the symbols of a phred+33 fastq file and the values are the associated fastq scores. I am getting a lot of errors relating to operators being expected throughout the entire code. The code runs fine without the hash portions.

Here is the code:

use warnings; 
use strict; 

open(IN, '<', '/path/to/in_file.txt') or die $!; 

my @symbols; 
my $count = 0; 
my %hash = (
    '!' => "0",  
    '"' => "1", 
    '#' => "2", 
    '$' => "3", 
    '%' => "4", 
    '&' => "5", 
    q(') => "6",
    '(' => "7", 
    ')' => "8", 
    '*' => "9", 
    '+' => "10", 
    ',' => "11", 
    '-' => "12", 
    '.' => "13", 
    '/' => "14", 
    '0' => "15", 
    '1' => "16", 
    '2' => "17", 
    '3' => "18", 
    '4' => "19", 
    '5' => "20", 
    '6' => "21", 
    '7' => "22", 
    '8' => "23", 
    '9' => "24", 
    ':' => "25", 
    ';' => "26", 
    '<' => "27", 
    '=' => "28", 
    '>' => "29", 
    '?' => "30", 
    '@' => "31", 
    'A' => "32", 
    'B' => "33", 
    'C' => "34", 
    'D' => "35", 
    'E' => "36", 
    'F' => "37", 
    'G' => "38", 
    'H' => "39", 
    'I' => "40", 
    'J' => "41", 
    'K' => "42"
);

while(my $fastq = <IN>){ 
    my $length = length $fastq;
    if ($length < 4180){ 
        next
    }
    my $substring = substr($fastq, 4175, 10);
    push(@symbols, $substring);
}
foreach (@symbols) { 
    my @eachsymbol = split //, $_;
    $count++;  
    print "Sequence ", $count, "\n"; 
    foreach my $symbol (@eachsymbol) { 
        if (exists $hash{$symbol}){
            print $hash{$symbol}, "\n"; 
        }
    } 
}       

For reference, the input file looks like: !''((((+))%%%++)(%%%%).1*-+*''))**

Thanks for your help.

4
  • 1
    ''' => "6", should be q(') => "6" Commented Nov 19, 2016 at 20:05
  • I updated my answer that fixes another issue, that single-quotes won't interpolate a variable. Commented Nov 19, 2016 at 20:06
  • This '$symbol' ... looks for the literal string consisting of $, s, y, m, b, o, and l in the hash. Clearly, such a key does not exist. Commented Nov 19, 2016 at 20:20
  • Thanks for your suggestions and help. Commented Nov 19, 2016 at 20:29

3 Answers 3

3

Whenever you are creating hash keys that contain any non-word characters (ie. not 0-9a-zA-Z_), you must quote them:

my $hash = (
    ';' => 1,
    '*' => 2,
    '!' => 'etc',
);

Same when you access/use them:

my $x = $hash{'!'};

Also, a variable will not interpolate when using single-quotes:

$hash{'$symbol'}

Change that to:

$hash{"$symbol"};

...or better:

$hash{$symbol};
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Stevieb, Thanks for the suggestion. I tried it and was still unsuccessful. I will edit my question with your suggestions to show you what I did.
2

You have

''' => "6",

in your hash definition which is problematic. You can use "'" or q{'} instead.

You could also save yourself and the people whose help you are asking some eye damage by defining %hash as:

my %hash = qw(
    !  0 
    "  1
    #  2
    $  3
    %  4
    ....
    H 39
    I 40
    J 41
    K 42
);

or even better:

my @sym = qw( ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K );

my %symmap = map +($sym[$_] => $_), 0 .. $#sym; 

This will result in the following warnings:

Possible attempt to put comments in qw() list ...

and

Possible attempt to separate words with commas ...

That's because the qw contains # and , characters. You can turn them off using

no warnings 'qw';

in the smallest applicable scope, as in

my %symmap;
{
    no warnings 'qw';
    my @sym = qw( ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K );
    %symmap = map +($sym[$_] => $_), 0 .. $#sym; 
}

Comments

1

To complete other answers, note that you can build your hash using the ascii codes (in this way you avoid the quoting problems):

my %symmap = map { (chr($_), $_ - 33) } 33..75;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.