in reply to Re: Extract the number of the species for txt file
in thread Extract the number of the species for txt file

Hi choroba

Thanks for your help. Your code works fine and can out put the all species and corresponding numbers to the screen. However, I just want to extract one specific specie and corresponding number at a time. Meanwhile, the corresponding number should be 0 if there is no such specific specie.And it would be better if the outcome can write into a new text file instead of the screen. Take O4 as an example, there should be just one output and should be like this

O4

0 # There is no “O4” specie in (1), so the corresponding number is 0.

3

0 # There is no “O4” species in (5), so the number is 0 too.

It will be grateful if you can help me.

Fan Li

  • Comment on Re^2: Extract the number of the species for txt file

Replies are listed 'Best First'.
Re^3: Extract the number of the species for txt file
by choroba (Cardinal) on May 05, 2016 at 09:05 UTC
    If you know the species in advance, there's no need to remember the counts for the remaining ones.
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; say my $search_for_species = 'O4'; while (<>) { my %counts; my @species = split; my @numbers = split ' ', <>; warn "Different number of columns at line $.!" if @species != @num +bers; for my $i (0 .. $#species) { $counts{ $species[$i] } = $numbers[$i]; } say $counts{$search_for_species} // 0; }

    To output to a file, use redirection:

    perl script_from_choroba.pl > new_file

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,