Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Seekers of Perl Wisdom

( #479=superdoc: print w/ replies, xml ) Need Help??

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
How do I select first string of a two dimensional array to compare to other values?
6 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


    #!/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-

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="" xml +ns="http:/ /" xsi:schemaLocation="http://www.ncbi. +nlm.nih.go v/SNP/docsum" 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>
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; );

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!

Can't close pipe to invalid process
2 direct replies — Read more / Contribute
by cLive ;-)
on Aug 28, 2015 at 17:37
    I was debugging why this test to create a PDF worked in the shell, but not from Jenkins (both environments running as the same user):
    my $fh; ok( open($fh, "|-", "prince - 2>/dev/null $test_file"), "Open +$test_file for writing" ); print $fh "<html><body><h1>Hello, world!</h1></body></html>"; ok( close($fh), "Close $test_file");

    Turns out it was because the PATH didn't exist in the Jenkins environment.

    Problem solved, but now I'm left scratching my head.

    ok 3 - Open /opt/humana/svn/checkouts/rosalind-repo/prod/tmp/testfile. +pdf for writing not ok 4 - Close /opt/humana/svn/checkouts/rosalind-repo/prod/tmp/test +file.pdf

    Why would the open succeed to an unknown executable? Is it because it dosen't fail until you attempt to use it? For some reason that feels a little off to me. What am I missing?

apache 2.4 mod_perl permission denied on standard modules
1 direct reply — Read more / Contribute
by Crackers2
on Aug 28, 2015 at 14:20

    I'm running on SLES12, with apache 2.4.10 and mod_perl 2.0.8

    In a vhost config file, I have a "PerlFixupHandler MyHandler" directive. is in /etc/apache2/script

    If I do a perl -e'use MyHandler' from the commandline, everything works fine. But when it gets used from the server, it fails and is logging this in the error_log:

    [Fri Aug 28 18:15:43.053198 2015] [perl:error] [pid 22133] [client x.x +.x.x:x] failed to resolve handler `MyHandler': Can't locate Digest/SH Permission denied at /etc/apache2/script/ line 7. +\nBEGIN failed--compilation aborted at /etc/apache2/script/MyHandler. +pm line 7.\nCompilation failed in require at (eval 5) line 2.\n, refe +rer: https://x.x.x.x/

    If I comment out the reference to Digest::SHA, it just fails on the next module.

    For debugging, I added a BEGIN block to dump INC and to try and read the file, and that does not appear to cause any errors.

    This configuration used to work fine on apache 2.2 ; I did some minimal updates to the configuration files for the new authentication stuff in 2.4, but nothing related to perl.

    Any idea on what's wrong and how to fix it?

    Edit: Here's the start of the .pm in question:

    package MyHandler; use strict; use warnings; use Apache2::Const qw(:common); use Digest::SHA qw(sha256_hex sha1_hex); use OtherModule; use Apache2::RequestRec; use Apache2::RequestIO; use Apache2::Const; use Apache2::Log; use APR::Table; use CGI::Cookie; use Apache2::URI (); use APR::URI; use APR::Pool ();
Given month last day
5 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 28, 2015 at 14:18
    Hi Monks!
    Can I pass a given month to Time::Piece and get its last day?
    #!/usr/bin/perl use strict; use warnings; use Time::Piece; use Time::Seconds qw(ONE_DAY ONE_WEEK ONE_MONTH ONE_YEAR); # Create Time::Piece New Object for the dates my $t = Time::Piece->new(); my $year = $t->year; # Pass November, but it can be any month my $month = "11"; my $last_day_month = $t->month_last_day($month); #?? print $last_day_month." days\n"; # 30

Add your question
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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (6)
    As of 2015-08-31 03:41 GMT
    Find Nodes?
      Voting Booth?

      The oldest computer book still on my shelves (or on my digital media) is ...

      Results (352 votes), past polls