Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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
variables in substition/eval
5 direct replies — Read more / Contribute
by xaphod
on Aug 17, 2017 at 18:41

    I run this script:

    use strict; use warnings; my @lines = ( "Once upon a time", "scrooge & donald", "went for a long walk" ); my @list = ("huey", "dewey", "louis"); foreach my $who (@list) { my $sub = "\$ln =~ s/scrooge (& donald)/$who \$1/g;"; print "$who\n"; foreach my $ln (@lines) { eval $sub; print "$ln\n"; } print "\n"; }

    and I get this:

    huey Once upon a time huey & donald went for a long walk dewey Once upon a time huey & donald went for a long walk louis Once upon a time huey & donald went for a long walk

    not what I was expecting. Anyone help?

    --
    TTFN, FNORD

    xaphod
Nested loops?
3 direct replies — Read more / Contribute
by Speed_Freak
on Aug 17, 2017 at 15:18

    I am working with existing code, and trying to add a "filter" to it. Currently the code pulls an id number and a sequence from a table in the database.(sql1) A foreach loop then permutes each sequence and searches the list to find out if any of the alternates exists. This leads to way more matches than I need because the loop eventually gets to the existing alternates, permutes them, and finds all of the matches again, just with a different primary sequence. I created a second database pull that creates another list with the same id-sequence layout that contains only the primary keys that I want to evaluate. (sql2) I want to use this secondary list as the filter for which sequences are evaluated from the primary list, but I need each key identified in the secondary list to be evaluated against all of the primary list.

    #currently have strict turned off #code snippet foreach my $sql1 (@{$sql1}) { $table1{$sql1->[1]}{$sql1->[0]}=undef; #rearranges the sql pull } foreach my $sql2 (@{$sql2}) { $table2{$sql2->[1]}{$sql2->[0]}=undef; #rearranges the sql pull } my %hash = (); my @array = (); my @bases = ('A','C','G','T'); foreach my $tar1 (keys %table1){ foreach my $tar2 (keys %table2) { if ($tar1 eq $tar2) { #a bunch of follow on code that works if the second foreach and if sta +tements are removed

    I'm just not sure which direction I should go with trying to limit the list it chooses to evaluate, without limiting the list of sequences it uses to evaluate against. I have tried several combinations of foreach/if/where statements and the closest I have gotten lead me to loop through the entire first table, but only using one sequence from the second table. I couldn't get it to iterate through the "filter" table. I'm sure my explanation is lacking severely.

Weird Date::Manip DateParse fail
1 direct reply — Read more / Contribute
by cormanaz
on Aug 17, 2017 at 14:32
    Good day bros. The following snippet:
    #!/usr/bin/perl -w use strict; use Date::Manip; use HTML::TreeBuilder; my $htm = ' <html><div class="posthead"> <span class="postdate new"><span class="date">14th August 2017,&nbsp;<span class= "time">21:07</span></span></span> <span class= "nodecontrols"><a name="post27949278" href= "threads/2360460-product-reviews.htm" class="postcounter">#1937</a></span> </div></html>'; my $tree = HTML::TreeBuilder->new_from_content($htm); my $postdate = $tree->look_down('class','date')->as_text(); print "postdate: $postdate\n"; print "postdate parsed: ",ParseDate($postdate),"\n"; my $timestamp = '14th August 2017, 21:07'; print "string parsed: ",ParseDate($timestamp),"\n";
    yields output:
    postdate: 14th August 2017, 21:07 postdate parsed: string parsed: 2017081421:07:00
    So it fails to parse a date when it's passed to ParseDate as the contents of a variable gotten with HTML::Element, but if I take the exact same text, assign it to a variable as a string literal, and pass it to ParseDate, it parses fine. I've debugged into Date::Manip and it seems to be getting the same string in both cases. Anyone know what's going on here?!?
LWP::UserAgent Get timing out after few request
2 direct replies — Read more / Contribute
by sannag
on Aug 17, 2017 at 10:58
    Update

    **************

    I don't the underlying cause. I fixed the issue on my program by using REST::Client instead of LWP::UserAgent....details of the code are in one of my reply below

    ***************

    My Get request is timing out after processing 20 or 30 request...I have over 500 records to process. I did confirm that there is no limit on number of GET requested placed to API. I also tried using chrom extension postman to repeatably place GET request successfully. Yet when I place GET request though my perl program it failed after processing few requests. Any help is greatly appreciated

    error says following:

    500 can't connect to api.xxx.com:443. (A connection attempt failed because the connected party did not properly respond after a periold of time, or established connection failed because connected host has failed to respond

    LWP::Protoclo::https::Socket: connect: a connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at C:/Dwimperl/perl/site/LWP/Protocol/http.pm"

    my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, ); $ua->timeout('1000'); sub getUserInfo { my($userId) = @_; my $partnerId = 'B53765B23456678C1' my $userURI = 'https://xxx.com/api/m1/request/'. $partnerId. '/users/ +'. $userId; print $userURI, "\n"; #sleep (3); my $userResponse = $ua -> request(GET($userURI, Authorization => $aut +hHeaders)); # I am getting a token which is passed in the header. if ($userResponse->is_success) { my $userRecord = decode_json($userResponse->content); print $userResponse->status_line, "\n"; return ($userRecord); } else { displayMsg ( "No user Records to process...................."); print $userResponse->status_line, "\n"; print $userResponse->decoded_content, "\n"; error ("Error: ", $userResponse->status_line, " " , $userResponse- +>decoded_content); exit 1; } }
match digit, followed by hyphen, followed again by digit - then add whitespaces and replace in file
4 direct replies — Read more / Contribute
by fasoli
on Aug 17, 2017 at 08:48

    Hi Wise Monks!

    I've been really confused about a problem I'm having with a file. It's a text file, with 4 columns, and with a few thousand lines. The contents are numbers that look like this

    1.234 5.6789 -1.235

    Those files occur as outputs from a software. The problem is that in some cases the contents look like this

    1.234 5.6789-12.235

    *notice the number of the last column: because now there are 2 numbers before the decimal point, the number gets stuck on the second column.

    Naturally now I'm having trouble plotting this file. So I'm trying to match strings where there is a digit, followed by a hyphen, followed by another digit, and then I want to replace this -hopefully correctly- with an added whitespace so that the numbers are correct.

    I'm trying this and the regex match works, it does print the problematic bits:.

    #!/usr/bin/perl use warnings; use strict; my $test; open my $INPUT, '<', "file.txt" or die $!; while (<$INPUT>) { chomp $_; if ($_=~/(\d)(-)(\d)/) { print "$1$2$3 \n"; } }

    But now I'm stuck: how do I complete the replace action? And how do I print the new contents of the file? I haven't succeeded in anything more than compilation errors. In terms of replacing, I've tried this

       if ($_=~s/(\d)(-)(\d)/(\d)    (-)(\d)/) {  

    (supposedly telling the script to add spaces between the digit before the hyphen and the hyphen itself)

    but I get this error

    Unrecognized escape \d passed through

    Then I tried it with the $1$2$3 but again it was wrong. Can you give me any hints about how to make the replace function work?? Thank you so much!

Error 500 in LWP
1 direct reply — Read more / Contribute
by YarNik
on Aug 17, 2017 at 06:25
    Hello! I'm trying to get a https page, half of the domains is OK. But with the other half I get the error:

    500 Can not connect to domen: 443 (connect: Network is unreachable); Client-Warning: Internal response

    The script is standard, I tried to change the agent and different versions of ssl_opts:
    use LWP::UserAgent; $ua = LWP::UserAgent->new( # agent => 'Mozilla/5.0 (X11; U; Linux i686 +; en-US; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5', # ssl_opts => { verify_hostname => 0, SSL_verify_mode => 0, + SSL_verifycn_scheme => 'none' }, ); $response = $ua->head("$get_url"); print $response->dump();
    P.s. domain example microsoft.com, google.com ( bing - it is OK. )
Find codon usage in domain based on domain's position from a file
4 direct replies — Read more / Contribute
by 345qwerty
on Aug 17, 2017 at 04:54

    Hello!

    I have two files with thousands of proteins: 1 file = protein ID + sequence of amino acids; 2 file = protein ID+sequence of nucleotides. And my third file is usual file with position of domains which are related with my proteins and nucleotides files. I related these three files with this code:

    acids.txt file contains:

    >ENST00000274849|Q9ULW3 MEAEESEKAATEQEPLEGTEQTLDAEEEQEESEEAACGSKKRVVPGIVYLGHIPPRFRPL HVRNLLSAYGEVGRVFFQAEDRFVRRKKKAAAAAGGKKRSYTKDYTEGWVEFRDKRIAKR VAASLHNTPMGARRRSPFRYDLWNLKYLHRFTWSH*

    FOR EXAMPLE

    nucleotides.txt file contains:

    >ENST00000274849|Q9ULW3 ATGGAGGCAGAGGAATCGGAGAAGGCCGCAACGGAGCAAGAGCCGCTGGAAGGGACAGAA CAGACACTAGATGCGGAGGAGGAGCAGGAGGAATCCGAAGAAGCGGCCTGTGGCAGCAAG AAACGGGTAGTGCCAGGTATTGTGTACCTGGGCCATATCCCGCCGCGCTTCCGGCCCCTG CACGTCCGCAACCTTCTCAGCGCCTATGGCGAGGTCGGACGCGTCTTCTTTCAGGCTGAG GACCGGTTCGTGAGACGCAAGAAGAAGGCAGCAGCAGCTGCCGGAGGAAAAAAGCGGTCC TACACCAAGGACTACACCGAGGGATGGGTGGAGTTCCGTGACAAGCGCATAGCCAAGCGC GTGGCGGCCAGTCTACACAACACGCCTATGGGTGCCCGCAGGCGCAGCCCCTTCCGTTAT GATCTTTGGAACCTCAAGTACTTGCACCGTTTCACCTGGTCCCACTGA

    domain.txt file contains:

    Q9ULW3;    46    142

    NOTE: this numbers mean the position domain in my sequence

    use strict; use Bio::SeqIO; #################################################### #MODULE 1: read protein file, and save it in a hash# #################################################### my %hash1; my $sequence = "acids.txt"; my $multifasta = Bio::SeqIO ->new (-file => "<$sequence",-format=> "fa +sta"); while (my $seq= $multifasta->next_seq()) { my $na= $seq->display_id; #Saves the ID in $na my $des=$seq->description; my $ss = $seq->seq; $hash1{$na} = $ss; } ############################################################# #MODULE 2: read nucleotide file, and save it in another hash# ############################################################# my %hash2; my $genes = "nucleotides.txt"; my $multifasta = Bio::SeqIO ->new (-file => "<$genes",-format=> "fasta +"); while (my $seq= $multifasta->next_seq()) { my $na= $seq->display_id; #Saves the ID in $na my $des=$seq->description; my $ss = $seq->seq; $hash2{$na} = $ss; } ##################### #MODULE 3: my $name;# ##################### my $name; # Read from standard input chomp $name; ###################################################################### +#### #MODULE 4: DOMAIN ANNOTATION + RELATED AMINO ACIDS AND NUCLEOTIDES IN +COLUMNS# ###################################################################### +#### foreach my $name (keys %hash1) { my $ac = (split(/\s*\|/, $name))[1]; #print "$ac\n" ; ################################################# #MODULE 4.1: DOMAIN ANNOTATION: POSITION OF DOMAINS# ################################################# open(FILE, "<" ,"domain.txt"); my @array = (<FILE>); my @lines = grep (/$ac/, @array); print for @lines; close (FILE); ####################################################### #MODULE 4.2: RELATED AMINO ACIDS AND NUCLEOTIDES IN COLUMNS# ######################################################## my @array1 = split(//, $hash1{$name}, $hash2{$name}); #CUT SEQUEN +CE my @array2 = unpack("a3" x (length($hash1{$name})),$hash2{$name}); + #CUT NUCLEOTIDE SEQUENCE my $number = "$#array1+1"; foreach (my $count = 0; $count <= $number; $count++) { print "$count\t@array1[$count]\t@array2[$count]\n"; } }

    And here is my FILE which I got after running the script:

    Q9ULW3; 46 142 0 M ATG 1 E GAG 2 A GCA 3 E GAG 4 E GAA 5 S TCG 6 E GAG 7 K AAG 8 A GCC 9 A GCA 10 T ACG 11 E GAG 12 Q CAA 13 E GAG 14 P CCG 15 L CTG 16 E GAA 17 G GGG 18 T ACA 19 E GAA 20 Q CAG 21 T ACA 22 L CTA 23 D GAT 24 A GCG 25 E GAG 26 E GAG 27 E GAG 28 Q CAG 29 E GAG 30 E GAA 31 S TCC 32 E GAA 33 E GAA 34 A GCG 35 A GCC 36 C TGT 37 G GGC 38 S AGC 39 K AAG 40 K AAA 41 R CGG 42 V GTA 43 V GTG 44 P CCA 45 G GGT 46 I ATT 47 V GTG 48 Y TAC 49 L CTG 50 G GGC 51 H CAT 52 I ATC 53 P CCG 54 P CCG 55 R CGC 56 F TTC 57 R CGG 58 P CCC 59 L CTG 60 H CAC 61 V GTC 62 R CGC 63 N AAC 64 L CTT 65 L CTC 66 S AGC 67 A GCC 68 Y TAT 69 G GGC 70 E GAG 71 V GTC 72 G GGA 73 R CGC 74 V GTC 75 F TTC 76 F TTT 77 Q CAG 78 A GCT 79 E GAG 80 D GAC 81 R CGG 82 F TTC 83 V GTG 84 R AGA 85 R CGC 86 K AAG 87 K AAG 88 K AAG 89 A GCA 90 A GCA 91 A GCA 92 A GCT 93 A GCC 94 G GGA 95 G GGA 96 K AAA 97 K AAG 98 R CGG 99 S TCC 100 Y TAC 101 T ACC 102 K AAG 103 D GAC 104 Y TAC 105 T ACC 106 E GAG 107 G GGA 108 W TGG 109 V GTG 110 E GAG 111 F TTC 112 R CGT 113 D GAC 114 K AAG 115 R CGC 116 I ATA 117 A GCC 118 K AAG 119 R CGC 120 V GTG 121 A GCG 122 A GCC 123 S AGT 124 L CTA 125 H CAC 126 N AAC 127 T ACG 128 P CCT 129 M ATG 130 G GGT 131 A GCC 132 R CGC 133 R AGG 134 R CGC 135 S AGC 136 P CCC 137 F TTC 138 R CGT 139 Y TAT 140 D GAT 141 L CTT 142 W TGG 143 N AAC 144 L CTC 145 K AAG 146 Y TAC 147 L TTG 148 H CAC 149 R CGT 150 F TTC 151 T ACC 152 W TGG 153 S TCC 154 H CAC 155 L CTC

    Now I should add a new fourth column which will contain 'YES' or 'NOT' - it depends on which codons are in domain - YES, which are not in domain - NOT. So, here is domains in the positions from 46 till 142. I would like to get this OUTPUT FILE:

    Q9ULW3; 46 142 0 M ATG NOT 1 E GAG NOT 2 A GCA NOT 3 E GAG NOT 4 E GAA NOT 5 S TCG NOT 6 E GAG NOT 7 K AAG NOT 8 A GCC NOT 9 A GCA NOT 10 T ACG NOT 11 E GAG NOT 12 Q CAA NOT 13 E GAG NOT 14 P CCG NOT 15 L CTG NOT 16 E GAA NOT 17 G GGG NOT 18 T ACA NOT 19 E GAA NOT 20 Q CAG NOT 21 T ACA NOT 22 L CTA NOT 23 D GAT NOT 24 A GCG NOT 25 E GAG NOT 26 E GAG NOT 27 E GAG NOT 28 Q CAG NOT 29 E GAG NOT 30 E GAA NOT 31 S TCC NOT 32 E GAA NOT 33 E GAA NOT 34 A GCG NOT 35 A GCC NOT 36 C TGT NOT 37 G GGC NOT 38 S AGC NOT 39 K AAG NOT 40 K AAA NOT 41 R CGG NOT 42 V GTA NOT 43 V GTG NOT 44 P CCA NOT 45 G GGT NOT 46 I ATT YES 47 V GTG YES 48 Y TAC YES 49 L CTG YES 50 G GGC YES 51 H CAT YES 52 I ATC YES 53 P CCG YES 54 P CCG YES 55 R CGC YES 56 F TTC YES 57 R CGG YES 58 P CCC YES 59 L CTG YES 60 H CAC YES 61 V GTC YES 62 R CGC YES 63 N AAC YES 64 L CTT YES 65 L CTC YES 66 S AGC YES 67 A GCC YES 68 Y TAT YES 69 G GGC YES 70 E GAG YES 71 V GTC YES 72 G GGA YES 73 R CGC YES 74 V GTC YES 75 F TTC YES 76 F TTT YES 77 Q CAG YES 78 A GCT YES 79 E GAG YES 80 D GAC YES 81 R CGG YES 82 F TTC YES 83 V GTG YES 84 R AGA YES 85 R CGC YES 86 K AAG YES 87 K AAG YES 88 K AAG YES 89 A GCA YES 90 A GCA YES 91 A GCA YES 92 A GCT YES 93 A GCC YES 94 G GGA YES 95 G GGA YES 96 K AAA YES 97 K AAG YES 98 R CGG YES 99 S TCC YES 100 Y TAC YES 101 T ACC YES 102 K AAG YES 103 D GAC YES 104 Y TAC YES 105 T ACC YES 106 E GAG YES 107 G GGA YES 108 W TGG YES 109 V GTG YES 110 E GAG YES 111 F TTC YES 112 R CGT YES 113 D GAC YES 114 K AAG YES 115 R CGC YES 116 I ATA YES 117 A GCC YES 118 K AAG YES 119 R CGC YES 120 V GTG YES 121 A GCG YES 122 A GCC YES 123 S AGT YES 124 L CTA YES 125 H CAC YES 126 N AAC YES 127 T ACG YES 128 P CCT YES 129 M ATG YES 130 G GGT YES 131 A GCC YES 132 R CGC YES 133 R AGG YES 134 R CGC YES 135 S AGC YES 136 P CCC YES 137 F TTC YES 138 R CGT YES 139 Y TAT YES 140 D GAT YES 141 L CTT YES 142 W TGG YES 143 N AAC NOT 144 L CTC NOT 145 K AAG NOT 146 Y TAC NOT 147 L TTG NOT 148 H CAC NOT 149 R CGT NOT 150 F TTC NOT 151 T ACC NOT 152 W TGG NOT 153 S TCC NOT 154 H CAC NOT 155 L CTC NOT

    This is example for one protein, I have to do it for thousands proteins. Please, do you have any suggestions?

    Thank you!

Inserting Variable Through Perl
2 direct replies — Read more / Contribute
by Abhay_Anand
on Aug 17, 2017 at 03:26

    Hi I am trying to insert a variable in 2nd line of a file. But it never takes variable value rather keeps taking it as text.

    root@vm-test-001:~/mongosearch# part=( "${arr[@]:i:batchsize}" ) root@vm-test-001:~/mongosearch# echo $part "C:00000092666270:53882159774" root@vm-test-001:~/mongosearch# perl -ni -e 'print; print "permissib +leCars = [ \${part[*]} ]\n" if $. == 2' query/containerId_count.js

    This keeps inserting $part as text like permissibleCars = [ ${part*} ] and not as variable like permissibleCars = "C:00000092666270:53882159774" How can this get fixed ?

Error ftping file with Net::FTP
2 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 16, 2017 at 10:34
    Hi Monks!

    I have a fileupload form submitting a file to this Perl script that I need to FTP once I have the file.
    I am getting and error, "No such file or directory".
    I am not passing the value correct from the fileupload into the FTP properly, I think.
    Any suggestion on how to get this right?
    The code sample:
    #!/usr/bin/perl use strict ; use warnings ; use CGI ; use Net::FTP; my $cgi = CGI->new(); my $file = $cgi->param( 'upload' ) || ''; do_ftp($file); sub do_ftp { my $filename = @_; return unless $filename ; $cgi->upload( 'upload' ); my $tmp = $cgi->tmpFileName( $filename ) ; rename( $tmp, $filename ) ; chmod 0664, $filename ; my $host = 'xxx'; my $user = 'user'; my $pass = 'pass'; my $ftpdir = '/'; my $ftp = Net::FTP->new($host, Debug => 0) or die "Could not connect to '$host': $@"; $ftp->login($user, $pass) or die sprintf "Could not login: %s", $ftp->message; $ftp->cwd($ftp_dir) or die sprintf "Could not login: %s", $ftp->message; # FTP file. $ftp->put($filename) or die "Cannot put file ", $ftp->message; $ftp->quit; }

    Thanks for Helping!
Reusing Compiled Perl or Opcode for Apache Server
3 direct replies — Read more / Contribute
by mlodato
on Aug 15, 2017 at 20:36

    O wise ones, I have come to you in humility to ask a question for which I have little surrounding knowledge. Please take pity on me and ask for clarification so I can seek information to bring back to you.

    I work on a project that has a Perl back end behind an Apache server. The development servers are very slow. For certain production-like servers, we precache our static content and preload our Perl modules. Doing this takes a long time. It would be nice if it didn't take so long a second time if the Perl code hasn't changed.

    I was wondering - is there was a way to preload all of the Perl modules once and then serialize it in some way to be read in more quickly a second time?

    Note that I don't yet know exactly what "preloading Perl modules" means, but I am actively looking into it and maybe you don't need that information to answer the question because preloading is a common term.

    I have seen several posts saying that compiling Perl into C-like code is not yet a thing. That's fine, I'm not looking to optimize or hide the code. I have seen several posts saying that I can generate an executable with PAR::Packer. Maybe this can be used? I have seen several posts saying that Perl is first parsed into opcode before being run. I'm not sure if this is just for Perl 6, but if it's for Perl 5, I see no reason for that opcode to not be reusable...right? I have seen a post explaining that Perl can't be parsed. I find this confusing.

    Edit: After some digging it looks to my untrained eyes like "preloading" just calls use, load_class from Class::Load, and ->new for each module


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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (3)
    As of 2017-08-18 11:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Who is your favorite scientist and why?



























      Results (300 votes). Check out past polls.

      Notices?