If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
Counting no of '>' symbol in a file
6 direct replies — Read more / Contribute
by GSperlbio
on Aug 31, 2015 at 06:27
    I have a file named seq.txt and i have hundreds of nucleotide sequences in fasta format each starting with symbol ">" i have to count no of > symbols to find how many seqences does that file has. can anyone write a perl code for that???
help to write perl code
4 direct replies — Read more / Contribute
by achs
on Aug 31, 2015 at 05:36
    I need help to write a perl script for printing out the initial values of the bit fields of a register as a 8 bit value. The information available in the file is like <reg> ... <bit id= 7> <name>A</name> <init_val>1</init_val> </bit> <bit id= 6> <name>B</name> <init_val>0<init_val> </bit> ...likewise bits upto 0 </reg> Likewise the file contains of 100 regs. I want to print out the 'init_val' of all the bit fields of the registers. I have tried writing the script while($line=<IN>) { .... #reg bit field if($line=~/\<bit\sid\=\"(.*)\"\>/) { $bit_name=$1; ... print "Line no. where the bit field has been found : $.\n\n"; #As 'init_val' occurs on the second line after 'bit id' $line_count=$.+2; print "The incremented value is $line_count\n\n "; #reading the initial_value print "####################\n\n\n"; while($sub_line=<IN>) { print "**********************Entered the second loop******************\n\n\n"; print "The line no is $.\n\n"; if($.==$line_count) { if($sub_line=~/\<INIT\_VAL\>(.*)\<\/INIT_VAL\>/) { $bits{"$bit_name"}=$1; print "...................The bit field initial value is $1......... \n\n" } if($sub_line=~/\<INIT\_VAL\/\>/) { $bits{"$bit_name"}=0; print "The bit field initial value is 0\n\n\n " } } print "End of the Loop\n\n"; %bits=qw(0 0); } print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n\n"; $.=$line_count; print "THE LINE COUNT IS $.\n\n "; } ... } But the above code is not working properly. After the first bit field,it is getting exited. It is not going to other bit fields of the reg as well as not other registers in the file
While is not working when using variable
2 direct replies — Read more / Contribute
by parthodas
on Aug 31, 2015 at 04:22
    I'm trying to run a while loop, after reading from a file. When I use variable, the while loop is not working. However when using absolute path, it is working fine. Please let me know of a way in which I can use the variable.
    # my $filename='$mydir\file_detail.txt'; # open (FILE, '$filename'); open (FILE, 'D:\SiebelAdmin\CSS\file_detail.txt'); while (<FILE>) {}
How do I select first string of a two dimensional array to compare to other values?
9 direct replies — Read more / Contribute
by joi1369
on Aug 30, 2015 at 19:07

    I have an array after splitting a text document into three different columns. I'm trying to retrieve the first value in the first column to compare against other values in that column and then print out that initial value but, when I use the following code, it prints all of the first column. I've tried several different methods of doing this but I think I'm just not understanding something. The code is below and any help is greatly appreciated. Thanks in advance! Input-

    1424621700, 2015-02-22 16:15:00, 4294.700 1424621760, 2015-02-22 16:16:00, 4289.700 1424621820, 2015-02-22 16:17:00, 4299.800 1424621880, 2015-02-22 16:18:00, 4302.800 1424621940, 2015-02-22 16:19:00, 4296.900 1424622000, 2015-02-22 16:20:00, 4301.000 1424622060, 2015-02-22 16:21:00, 4300.300

    Script-

    #!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0]; open (RAW, "./$file") or die "Can't open $file for read: $!"; open (OUT, ">./OUTPUT_$file") or die "Can't open Output_$file: $!"; while(<RAW>) { my $line = $_; chop($line); next if ($line =~ /!/); my @splits = split (',', $line); my $first_value = @splits[0]; print "$first_value\n"; }

    Wanted output-

    1424621700
Print XML elements and attributes
1 direct reply — Read more / Contribute
by AhmedABdo
on Aug 30, 2015 at 11:20
    Hi all, I have this simple perl script working on an xml file, I want to print two attributes
    and one element, I just got two attributes, the element "observed", is not printed
    Could you help me to fix it please this is my code
    Thanks in advance =========================
    use XML::Twig; my $file = 'sample.xml'; my $twig = XML::Twig->new(); $twig->parsefile($file); my $root = $twig->root; foreach my $species ($root->descendants('Ss')) { print $species->att('ssId'); print "\t"; print $species->att('handle'); print "\t"; print $species->next_sibling_text('Observed'); print "\n"; }
    ======================== This is part of the file ======================
    <?xml version="1.0" encoding="UTF-8"?> <ExchangeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml +ns="http:/ /www.ncbi.nlm.nih.gov/SNP/docsum" xsi:schemaLocation="http://www.ncbi. +nlm.nih.go v/SNP/docsum ftp://ftp.ncbi.nlm.nih.gov/snp/specs/docsum_3.4.xsd" spec +Version="3 .4" dbSnpBuild="144" generated="2015-05-26 09:54"> <SourceDatabase taxId="9606" organism="human" gpipeOrgAbbr="hs"/> <Rs rsId="3894" snpClass="snp" snpType="notwithdrawn" molType="gen +omic" genotype="true" bitField="050028000005130500030100" taxId="9606 +"> <Het type="est" value="0.05" stdError="0.1547"/> <Validation byCluster="true" byOtherPop="true" byHapMap="true" + by1000G=" true"> <otherPopBatchId>7179</otherPopBatchId> </Validation> <Create build="36" date="2000-09-19 17:02"/> <Update build="144" date="2015-05-07 10:52"/> <Sequence exemplarSs="491581208" ancestralAllele="C,C"> <Seq5>ATAAGCAAATAACTGAAGTTTAATCAGTCTCCTCCCAGCAAGTGATATGCAA +CTGAGATTCC TTATGACACATCTGAACACTAGTGGATTTGCTTTGTAGTAGGAACAAGGTACATTCGCGGGATAAATGTG +GCCAAGTTTT ATCTGCTGCCAGGGCTTTCAAATAGGTTGACCTGACAATGGGTCACCTCTGGGACTGA</Seq5> <Observed>C/T</Observed> <Seq3>AATTAGGAAGAGCTGGTACCTAAAATGAAAGATGCCCTTAAATTTCAGATTC +ACAATTTTTT TTTCTTAGTATAAGCATGTCCCATGTAATATCTGGGATATACTCATACCTTTAAAAATGTGCTCATTGTT +TATCTGAAAT TCACATTTTAACAGGGAACCATTGTTTTGTTATTGTTTATTGTTTTGTTTCTAAATAA</Seq3> </Sequence> <br> <Ss ssId="3931" handle="OEFNER" batchId="489" locSnpId="M +3" subSnpClass="snp" orient="forward" strand="bottom" molType="genomi +c" buildId="36" methodClass="DHPLC" validated="by-cluster"> <Sequence> <Seq5>TAATCAGTCTCCTCCCAGCAAGTGATATGCAACTGAGATTCCTTATGA +CACATCTGAACACTAGTGGATTTGCTTTGTAGTAGGAACAAGGTACATTCGCGGGATAAATGTGGCCAA +GTTTTATCTGCTGCCAGGGCTTTCAAATAGGTTGACCTGACAATGGGTCACCTCTGGGA CTGA</Seq5> <Observed>C/T</Observed> <Seq3>AATTAGGAAGAGCTGGTACCTAAAATGAAAGATGCCCTTAAATTTCAG +ATTCACAATTTT</Seq3>
    </Sequence>
inverting keys in a 2-D Hash
3 direct replies — Read more / Contribute
by melmoth
on Aug 30, 2015 at 09:32
    Hi I have a 2D hash of arrays like: $HASH{keyA}{keyB} = [1,2,3,4]; and want to turn it into this: $HASH{keyB}{keyA} = [1,2,3,4]; and was wondering what the cleanest way to this is. thnaks!
exit value in tar command
5 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 30, 2015 at 03:06
    Hi perl monks,

    In a perl script i am calling a ksh file to copy files. (ie.) inside ksh file i am running the below tar command to copy the files from source to destination.

    My task is, Need to create a 'success.ok' file, if tar command got succeed, else create 'failure.ok'. The issue is, while running ksh file through perl script, by default i am getting the exist value as '0' for both failure and success scenario..

    KIndly please suggest how to solve this.

    (tar cvf - * | cd /var/tmp/dump; tar xcf - ); output=\$\?; if \$output -eq 0; then touch /var/tmp/dump/success.ok; );

    (tar
how to change this code into perl
2 direct replies — Read more / Contribute
by perlnewbie012215
on Aug 30, 2015 at 02:44

    awk 'NR==FNR{a$1++;next;}{ if (a$1 > 1)print;}' imageoutcome.txt imageoutcome.txt

    this code prints duplicate rows basing on the first column, can you guys help me write in perl

Digest::MD5 addfile() w/ tied filehandle
1 direct reply — Read more / Contribute
by jdv
on Aug 29, 2015 at 23:24

    I'm observing an odd interaction of my module with Digest::MD5 that I haven't been able to figure out.

    I'm implementing a read/write interface to a gzip variant using tied filehandles, with full seek/read/readline/tell support. I'm in the testing phase and nearly all of the tests I'm throwing at it seem to be working (basically performing exactly the same combinations of seek, read, <>, etc, on both my tied filehandle object (with the compressed file loaded) and a regular Perl filehandle opened on the uncompressed version, and comparing the output).

    The one exception at this point is when I try to provide my tied filehandle to Digest::MD5's addfile() method. This doesn't work:

    my $fh = B2B::BGZF::Reader->new_filehandle( $fn_bgzf ); my $hex = Digest::MD5->new()->addfile($fh)->hexdigest; print $hex, "\n"; # prints d41d8cd98f00b204e9800998ecf8427e

    The test returns almost immediately and it appears the hash returned is that of an empty string, so clearly the file is not actually being read. However, this works as expected:

    my $fh = B2B::BGZF::Reader->new_filehandle( $fn_bgzf ); my $d = Digest::MD5->new(); my $buf = ''; $d->add($buf) while ( read $fh, $buf, 4096 ); my $hex = $d->hexdigest; print $hex, "\n"; # prints the expected sum

    It also works fine if I run the original code but substitute the pure-Perl module (although painfully slowly):

    my $fh = B2B::BGZF::Reader->new_filehandle( $fn_bgzf ); my $hex = Digest::Perl::MD5->new()->addfile($fh)->hexdigest; print $hex, "\n"; # prints the expected sum

    I additionally tried it with Digest::SHA, and that works fine too:

    my $fh = B2B::BGZF::Reader->new_filehandle( $fn_bgzf ); my $hex = Digest::SHA->new(1)->addfile($fh)->hexdigest; print $hex, "\n"; # prints the expected sum

    Basically, I have only been able to observe the issue when using my module with the XS implementation of Digest::MD5. Debugging is difficult because I'm not sure what code is actually being called (apparently not the addfile() method of Digest::base or any other actual perl code I can find on my system). I have no problem just using the explicit read()/add() form with Digest::MD5, but if this is an indication of a subtle bug in my code I'd like to work it out - I'm just not sure how to do so.

    Any help with understanding what Digest::MD5::addfile() is actually calling under the hood or what might be going on here would be greatly appreciated.

regex match counter
1 direct reply — Read more / Contribute
by OldChamp
on Aug 29, 2015 at 16:43

    How can I get a match counter and use it as a prefix in the peplace term. For example, I have several times "position" in my text and want to peplace it with "position 1", "position 2" and so on. Many thanks in advance!


Add your question
Title:
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.