Domanda

I have one file, which contain 12 columns (bam file), the eleventh column contain ASCII code. In one file I have more than one ASCII code. I would like to convert it to number. I think it this code:

perl -e '$a=ord("ALL_ASCII_CODES_FROM-FILE"); print "$a\t"'

And I would like to create for cycle to read all ASCII codes, which are in eleventh column, convert it to number and count the results to one number.

È stato utile?

Soluzione

You need to split the string into individual characters, loop over every character, and call ord in the loop.

my @codes = map ord, split //, $str;
say join '.', map { sprintf("%02X", $_) } @codes;

Conveniently, unpack 'C*' does all of that.

my @codes = unpack 'C*', $str;
say join '.', map { sprintf("%02X", $_) } @codes;

If you do intend to print it out in hex, you can use the v modifier in a printf.

say sprintf("%v02X", $str);

Altri suggerimenti

The natural tool to convert a string of characters into a list of the corresponding ASCII codes would be unpack:

my @codes = unpack "C*", $string;

In particular, assuming that you're parsing the QUAL column of an SAM file (or, more generally, any FASTQ-style quality string, I believe the correct conversion would be:

my @qual = map {$_ - 33} unpack "C*", $string;

Ps. From your mention of "columns", I'm assuming you're actually parsing a SAM file, not a BAM file. If I'm reading the spec correctly, the BAM format doesn't seem to use the +33 offset for quality values, so if you are parsing BAM files, you'd simply use the first example above for that.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top