Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Seekers of Perl Wisdom

by gods
on Sep 07, 1999 at 20:28 UTC ( #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
Win32 OLE Word Add Password?
No replies — Read more | Post response
by cormanaz
on Sep 27, 2016 at 12:53
    Good day bros. Does anyone know how to add a password to a Word doc using Win32 OLE?
Issues with substr and substituting
2 direct replies — Read more / Contribute
by lecb
on Sep 27, 2016 at 12:01

    Dear Monks, Please may I kindly ask for some assistance please? Please be gentle however.. I am not an expert coder and I am also incredible inefficient, but for now I have to code in a way that makes logical sense to me. I have tried my best for 5 days with this and now I'm at the point where I need some external help...

    The input file is commented in the code since I can't upload it for you. Essentially what I am trying to do is change the last column of the input (a 9 letter sequence) by changing each letter to an alternative of 3 letters AGCT, then moving onto the next letter in the sequence.

    I.e. for each character of the last column, if it matches A, then switch to G then print new line, switch to T then print new line, switch to C then print new line (whilst remaining sequence stays same).

    Example 1: for >chr1 67093601 67093610 CTACAGGAA -> switch to GTACAGGAA, TTACAGGAA, ATACAGGAA.

    Then: CTACAGGAA -> switch to CAACAGGAA, CGACAGGAA, CCACAGGAA etc.

    Therefore, for each sequence there should be 27 new sequences.

    From looking at the output, I think the issue lies in the "$sequence =~ s/$test/C/;" bit of code, but I don't know how to fix it... the $j seems to count along the string as I'd like it to, but sometimes the wrong letter in the sequence in changed, and sometimes it is not!

    If anyone can help me, I will be very, very grateful.

    BW LECB

    #!/usr/bin/perl use strict; use Data::Dumper; use Storable; ## 5_prime_ref.fasta => # >chr1 67093601 67093610 CTACAGGAA # >chr1 67095418 67095427 TTTCAGAAA # >chr1 67096318 67096327 TTTCAGCTC # >chr1 67115461 67115470 TTCCAGGCA # >chr1 67125906 67125915 CTGCAGATA # >chr1 67127254 67127263 AAATAGGAA # >chr1 67131224 67131233 TAATAGATT # >chr1 67093601 67093610 CTACAGGAA # >chr1 67096318 67096327 TTTCAGCTC # >chr1 67103379 67103388 GTTTAGATG # >chr1 67111641 67111650 CTTTAGAGC # >chr1 67115461 67115470 TTCCAGGCA # >chr1 67125906 67125915 CTGCAGATA # >chr1 67127254 67127263 AAATAGGAA # >chr1 67131224 67131233 TAATAGATT # >chr1 67093601 67093610 CTACAGGAA # >chr1 67096318 67096327 TTTCAGCTC # >chr1 67103379 67103388 GTTTAGATG # >chr1 67111641 67111650 CTTTAGAGC # >chr1 67113753 67113762 ATATAGTGA # >chr1 201324579 201324588 GGGTAAGGT open my $fasta5, '<', '5_prime_ref.fasta'; chomp (my @fasta5 = <$fasta5>); close $fasta5; my ($altsequence1, $altsequence2, $altsequence3, @newfasta5, @newfasta +3, $sequence, @splity, $test, $j, $genomicposition, $ref, $alt1, $alt +2, $alt3, $refsequence); ## for 5 prime ## >chr1 201324579 201324588 GGGTAAGGT foreach my $line (@fasta5) { ## need to iterate 9 times through j... $j = 0; while ($j < 9) { @splity = split ("\t", $line); my $ref = $splity[3]; $sequence = $splity[3]; $refsequence = $splity[3]; $genomicposition = $splity[1]; $genomicposition = $genomicposition + 1; #first nucleotide is coun +ted as first column + 1, not as the left boundary $splity[0] = ">".$splity[0]; ##if ($character $j in sequence =~ matches A) { if (substr ($sequence, $j, 1) =~ m/A/) { $ref = "A"; $genomicposition = $genomicposition + $j; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/C/; $altsequence1 = $sequence; $alt1 = "C"; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/G/; $altsequence2 = $sequence; $alt2 = "G"; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/T/; $altsequence3 = $sequence; $alt3 = "T"; } elsif (substr ($sequence, $j, 1) =~ m/C/) { $test = substr ($sequence, $j, 1); $ref = "C"; $genomicposition = $genomicposition + $j; $sequence =~ s/$test/A/; $altsequence1 = $sequence; $alt1 = "A"; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/G/; $altsequence2 = $sequence; $alt2 = "G"; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/T/; $altsequence3 = $sequence; $alt3 = "T"; } elsif (substr ($sequence, $j, 1) =~ m/T/) { $test = substr ($sequence, $j, 1); $ref = "T"; $genomicposition = $genomicposition + $j; $sequence =~ s/$test/A/; $altsequence1 = $sequence; $alt1 = "A"; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/G/; $altsequence2 = $sequence; $alt2 = "G"; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/C/; $altsequence3 = $sequence; $alt3 = "C"; } else { $ref = "G"; $genomicposition = $genomicposition + $j; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/A/; $altsequence1 = $sequence; $alt1 = "A"; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/T/; $altsequence2 = $sequence; $alt2 = "T"; $test = substr ($sequence, $j, 1); $sequence =~ s/$test/C/; $altsequence3 = $sequence; $alt3 = "C"; } ## print the new 3 lines my $newfasta5alt1 = join ("\t", $refsequence, $splity[0], $splity[ +1], $splity[2], $altsequence1, $ref, $alt1, $genomicposition); my $newfasta5alt2 = join ("\t", $refsequence, $splity[0], $splity[ +1], $splity[2], $altsequence2, $ref, $alt2, $genomicposition); my $newfasta5alt3 = join ("\t", $refsequence, $splity[0], $splity[ +1], $splity[2], $altsequence3, $ref, $alt3, $genomicposition); my $newfasta5 = join ("\n", $newfasta5alt1, $altsequence1, $newfas +ta5alt2, $altsequence2, $newfasta5alt3, $altsequence3); push (@newfasta5, $newfasta5); $j++;} next; } print Dumper @newfasta5;
Perl script to run for x hours
3 direct replies — Read more / Contribute
by t-rex
on Sep 27, 2016 at 06:41

    Hello Monks,

    In my script i give user a parameter where he mentions he wants to run this script for x number of hours, how should i implement this? I read online , one way could be using SIGALRM and writing handler for this so that when this is achieved I stop the script,

    please suggest other efficient ways if there exist, thanks

error : can't locate object method "getAttribute" via package XML::DOM::NodeList
2 direct replies — Read more / Contribute
by ankit.tayal560
on Sep 27, 2016 at 04:02
    use strict; use warnings; use Data::Dumper; use XML::DOM; my $parser=new XML::DOM::Parser; my $doc=$parser->parsefile('C:\perl\perl_tests\xmlin.xml') or die$!; my $config=$doc->getElementsByTagName("config"); my $config_attr=$config->getAttribute("logdir"); print("$config_attr");

    XML file :<config logdir="var/log/foo/" debugfile="tmp/foo.debug"> <server name ="sahara" osname ="solaris" osversion="2.6"> <address>10.0.0.101</address> <address>10.0.1.101</address>Text </server> <server name="gobi" osname="irix" osversion="6.5"> <address>10.0.0.102</address>text1 </server> <server name="kalahari" osname="linus" osversion="2.0.34"> <address>10.0.0.103</address> <address>10.0.1.103</address>Text2 </server> </config> when I try to run this script I get an error saying : can't locate object method "getAttribute" via package "XML::DOM::NodeList" at C:\perl\perl_tests\xmlin.pl line 10. what is the problem with my script?

One-liner's quoting and subroutine
3 direct replies — Read more / Contribute
by reisinge
on Sep 27, 2016 at 01:42

    Dear Monks :-), I have two questions related to the following one-liner:

    find /opt/splunk/syslog/ -iname "*log*" -type f -mtime +30 | perl -wne + 'BEGIN { $sum = 0 }; chomp; $sum += (stat)[7]; END { print "$sum\n" +}'

    1) How should I quote it (escape the single quotes) when I want to execute it on a remote machine via SSH? Ex. ssh root@HOST 'ONE-LINER'

    2) Is there is a (smart) way to incorporate the following subroutine into the one-liner?

    sub scaleIt { my $size_in_bytes = shift; return unless defined $size_in_bytes; my ( $size, $n ) = ( $size_in_bytes, 0 ); ++$n and $size /= 1024 until $size < 1024; return sprintf "%.0f%s", $size, (qw[ B KB MB GB TB ])[$n]; }

    You can talk and talk and have great ideas, but if you don't have a way of sharing those ideas then you are just saying it in you own bubble. -- Tom Limoncelli
how to jump to some specific line in my code ?
6 direct replies — Read more / Contribute
by ankit.tayal560
on Sep 27, 2016 at 00:51
    use strict; use warnings; print("Enter the number of flags which needs to be generated..\n"); my $num=<STDIN>;

    this is some part of my code. If the input number is 0 or less than 0 than it should jump to some other part say line number 32 of my code. how can I achieve that? any specific function which I can use?

Win 10 Anniversary killed Perl
1 direct reply — Read more / Contribute
by cxgy
on Sep 26, 2016 at 23:20
    Help! I've been running ActivePerl for years now & have scores of scripts running to manipulate data & update websites. Last night Windows 10 did a major update to Build 1607 (the 'Anniversary Edition'). Perl no longer works! The cmd window opens but nothing happens. When clicking on a .pl file, I get a UAC warning about 'no publisher' and then the same dead cmd window. I tried installing Strawberry. That eliminated the UAC warning, but still won't run. I've had to fire up my Win 7 machine as backup. Has this happened to anyone else?
compare two files on the basis of Two IDs
2 direct replies — Read more / Contribute
by genome
on Sep 26, 2016 at 17:26
    Hi, I want to compare two files on the basis of two different Ids, and want to print something, if the Ids matched.

    I opened the two files and used while loop (i.e. while within while). The if condition of this prog. is working good, but else condition will give lots of lines, because of while within while.. "last;" is also not working here..

    Please help me to solve this

    file 1

    chr17 69112551 chr7 151046672 chr7 151047369
    file 2
    chr1 66953622 66953654 chr1 67200451 67200472 chr1 67200475 67200478 chr1 67058869 67058880 chr1 67058881 67058885 chr1 67058887 67058895

    Perl prog.

    #!/usr/bin/perl -w $pwd = `pwd`; chomp($pwd); $file=$ARGV[0]; $file1=$ARGV[1]; open(IN,$file); while ($line=<IN>){ chomp($line); @ary = split(/\t/,$line); chomp($ary[0]);chomp($ary[1]); open(SK,$file1); while($line1=<SK>) { @any = split(/\t/,$line1); chomp($any[0]); chomp($any[0]);chomp($any[1]);chomp($any[2]); if ($ary[0] eq $any[0] and $ary[1] == $any[1]) or ($ary[0] eq $any[0] +and $ary[1] == $any[2]) { print "$line\tE\n"; } else { print "$line\tM\n"; } } }
why are hex values not numbers?
5 direct replies — Read more / Contribute
by perl-diddler
on Sep 26, 2016 at 16:36
    Why are hex values not interpreted as numbers:
    printf "dec(%d) hex(%x)", "65", "0x41"; Argument "0x41" isn't numeric in printf at -e line 2. dec(65) hex(0) # vs. shell: > printf "dec(%d) hex(0x%08x)\n" "65" "0x41" dec(65) hex(0x00000041)

    I know about hex, that doesn't explain why perl shouldn't be able to DWIM, no?

    p.s. maybe the latest perl should allow "0x41"->$* ? (tongue-in-cheek)

Online Tutorial To Get Started
6 direct replies — Read more / Contribute
by Svetlana
on Sep 26, 2016 at 15:25
    As a followup to my previous thread(http://www.perlmonks.org/?node_id=1172488) I was wondering if someone could recommend a good on line tutorial to get started with Perl. I come from a background of Java/C# and I understand the concept of classes/polymorphism etc.. I know I said that I don't trust websites and prefer books, but, it's going to be some time before I get my hands on Learning Perl.

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (9)
    As of 2016-09-27 19:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Extraterrestrials haven't visited the Earth yet because:







      Results (511 votes). Check out past polls.