namhsuya
namhsuya

Reputation: 117

What does $variable{$2}++ mean in Perl?

I have a two-column data set in a tab-separated .txt file, and the perl script reads it as FH and this is the immediate snippet of code that follows:

while(<FH>)
{
    chomp;
    s/\r//;
    /(.+)\t(.+)/;
    $uniq_tar{$2}++;
    $uniq_mir{$1}++;
    push@{$mir_arr{$1}},$2;
    push @{$target{$2}} ,$1;
}

When I try to print any of the above 4 variables, it says the variables are uninitialized. And, when I tried to print $uniq_tar{$2}++; and $uniq_mir{$1}++; It just prints some numbers which I cannot understand.

I would just like to know what this part of code evaluate in general? $uniq_tar{$2}++;

Upvotes: 1

Views: 728

Answers (1)

Dave Cross
Dave Cross

Reputation: 69314

The while loop puts each line of your file, in turn, into Perl's special variable $_.

/.../ is the match operator. By default it works on $_.

/(.*)\t(.*)/ is a regular expression inside the match operator. If the regex matches what is in $_, then the bits of the matching string that are inside the two pairs of parentheses are stored in Perl's special variables $1 and $2.

You have hashes called %uniq_tar and %uniq_mir. You access individual elements in a hash using the $hashname{key}. So, $uniq_tar{$1} is finding the value in %uniq_tar associated with the key that is stored in $1 (that is - the part of your record before the first tab).

$variable++ increments the number in $variable. So $uniq_tar{$1}++ increments the value that we found in the previous paragraph.

So, as zdim says, it's a frequency counter. You read each line in the file, and extract the bits of data before and after the first tab in the line. You then increment the values in two hashes to count the number of occurences of each of the strings.

Upvotes: 3

Related Questions