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
Compare variables when the number of them vary
2 direct replies — Read more / Contribute
by stroke
on May 03, 2015 at 12:52

    Hi, this script queries the defined list of DNS servers and grabs the serial numbers for the listed zones. It then compares them against each other and logs if they match or not. It may not be pretty, but it works !

    But, the bit I'm struggling with now is how best to do the serial number comparison if the number of DNS server in the list changes - that is if I add in more or remove DNS server IPs to the list, I don't have to manually go and add/remove the serial number references for the check - I want to make the serial compare "dynamic", based on the number of DNS servers.

    I'm okay with just comparing all other serials to the first serial number, rather than comparing all possible combinations.

    fyi - I'm writing all the results out to a file for reference, in addition to sending errors only to STDOUT. Thanks

    #!/usr/bin/env perl use strict; use warnings; use Net::DNS; use 5.010; my @nameservers = qw(8.8.8.8 4.2.2.2); my @errors; open(my $resultsfh, '>', "results-serials.csv") or die "cannot open > results-serials.csv: $!"; print $resultsfh "Result,Zone"; foreach (@nameservers) { print $resultsfh ",Serial for: $_"; } print $resultsfh "\n"; while (<DATA>) { my $fqdn = $_; chomp $fqdn; my @serials; my $res = Net::DNS::Resolver->new; $res->tcp_timeout(2); $res->udp_timeout(2); foreach my $ns (@nameservers) { $res->nameservers($ns); my $reply = $res->query("$fqdn", "SOA"); if ($reply) { push(@serials, ($reply->answer)[0]->serial); } else { push(@serials, $res->errorstring); } } # Compare Serials no warnings 'numeric'; if ($serials[0] == $serials[1]) { say $resultsfh "MATCH,$fqdn,$serials[0],$serials[1]"; } else { say $resultsfh "MISMATCH,$fqdn,$serials[0],$serials[1]"; push @errors, "MISMATCH.$fqdn,$serials[0],$serials[1]"; } } if (@errors) { say "ERRORS:"; foreach (@errors) { say "$_"; } } __DATA__ google.com apple.com
Error on Windows 7: Message: The application has failed to start because its side-by-side configuration is incorrect.
2 direct replies — Read more / Contribute
by psiciliano
on May 03, 2015 at 10:22

    Hi Monks,

    I'm modifying an open source program (Slic3r) and when I've successfully package my new program with citrus perl 5.16 and CAVA packager on windows

    But when I tried to run it on my client's machine the program fail with the error "Message: The application has failed to start because its side-by-side configuration is incorrect.". If I understand it correctly, it's caused because some library version on my client's machine are diferent from the libraries that I've used to compile XS modules. It not surprices me, because I've compiled it with Mingw, and they has installed Visual Studio.

    I think it can be solved compiling it statically (Including all libraries on the .exe file) but there is a question ... How can I do that?

    Sorry for my bad English (It's not my first language) or if it's not specifically a Perl question ... But really I think this needs Perl wisdom

    Regards.
    Pablo.

EXTRACT FASTA SEQUENCE FROM ORIGINAL FASTA FILE DEPENDING ON ANOTHER SEQUENCE HEDAER FILE
2 direct replies — Read more / Contribute
by rameshchand123
on May 03, 2015 at 10:20
    I have two files: File 1: contain many FASTA sequences >84C2_Locus_14_Transcript_1/3_Confidence_0.571_Length_1244 AAACTAGTCAATAGAGAAAATCCAAAGTGGATGAAATTGAAGTGATTGTATGGCACAAGT...so on

    >84C2_Locus_14_Transcript_2/3_Confidence_0.857_Length_1961 AAACTAGTCAATAGAGAAAATCCAAAGTGGATGAAATTGAAGTGATTGTATGGCACAAGT...so on

    >84C2_Locus_15_Transcript_1/9_Confidence_0.190_Length_757 ATTTGCTCGGAAAAACACTTCTCGTGGAACTTGTTAGCGCTGAGCTTGATCCCAAGACGA.....so on

    >84C2_Locus_15_Transcript_5/9_Confidence_0.333_Length_1841 ATTTGCTCGGAAAAACACTTCTCGTGGAACTTGTTAGCGCTGAGCTTGATCCCAAGACGA....so on

    File2: only the sequence headers so many

    84C2_Locus_14_Transcript_1/3_Confidence_0.571_Length_1244 84C2_Locus_14_Transcript_2/3_Confidence_0.857_Length_1961 84C2_Locus_14_Transcript_3/3_Confidence_0.571_Length_1248 84C2_Locus_15_Transcript_1/9_Confidence_0.190_Length_757 ........ many more

    my output file should be contain the sequence associated with header. i.e. matching the sequence header file (file2) header portion with original fasta sequence file (file1) and those headers match the fasta sequence header in file 1 only those sequences store in another output file containing header with sequence.Just like this: Original output file should like this:

    >84C2_Locus_15_Transcript_5/9_Confidence_0.333_Length_1841 ATTTGCTCGGAAAAACACTTCTCGTGGAACTTGTTAGCGCTGAGCTTGATCCCAAGACGA......so on

    Please suggest me the way in perl which is applicable for my problem.

Proper location of module required for test scripts
2 direct replies — Read more / Contribute
by gflewis
on May 03, 2015 at 10:10
    I am trying to package my first module for CPAN distribution. It is pretty simple: just a single module; but there are about 20 test scripts. I am using Module::Build. I have a small file ("test.config") of configuration data that is required for most of my test scripts, and a small module ("TestUtil.pm") that reads this configuration data. Currently these two files are in the "t" directory. However, when I type ./Build test it can't locate TestUtil.pm. What is the proper location for a module whose only purpose is to support the test scripts?
Random parttitions?
3 direct replies — Read more / Contribute
by BrowserUk
on May 03, 2015 at 08:35

    Given start, end & count, I want to generate an ordered set of count random values between start and end, that partition that range roughly equally. (Not exactly; then they wouldn't be random!)

    So, I can do something like this:

    sub rndPart{ my( $start, $end, $count ) = @_; sort{ $a <=> $b } map{ sprintf "%.2f", $start + rand( $end - $start ) } 1 .. $count };; print rndPart( 1, 10, 10 ) for 1 .. 10;; 1.26 1.33 2.13 3.69 5.22 5.30 6.16 7.02 9.69 9.98 2.51 3.97 5.80 6.10 6.13 7.30 8.33 8.70 9.87 9.99 3.59 4.94 6.21 6.88 7.23 7.53 8.30 8.36 9.84 9.91 2.97 3.38 4.01 4.81 5.04 5.42 5.99 6.42 8.23 9.06 1.10 2.86 3.12 4.09 4.35 4.38 7.29 7.46 9.02 9.48 1.01 2.70 3.15 3.23 4.47 6.56 7.62 8.84 9.16 9.52 1.08 1.63 3.44 4.22 5.08 5.58 6.34 7.40 7.69 9.40 3.29 3.66 4.44 4.54 4.74 4.81 5.02 5.53 6.53 8.09 1.44 2.34 2.51 4.77 5.74 6.64 8.59 8.80 8.98 9.28

    Which isn't too bad, but:

    1. There are no guarantees about the distribution:

      They could all end up clustered at one end, or the other, or bunched up in the middle some where.

    2. The need to sort is a drain on resources if the sets are large.

      It also means that the values are strictly increasing; and whilst I want the general trend to be so; I'd like to allow for the occasional blip above or below.

    Is there a better way? Can the sort be avoided? Can the distribution be regularised, with being either rigid; or precluding the occasional skewed set?

    I've thought about generating a set of exact partitions, and then applying a randomised delta, but the implementation isn't falling off the page for me today.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Using Expressions
3 direct replies — Read more / Contribute
by programmercarlito
on May 02, 2015 at 19:08
    If I wanted to extract errors from a file how would I do it? I know the basics but not sure on this one because some error messages go more than one line. I was storing each line in array but I cant get the complete line. I saw there is a space before the into in each line. I dont know if that will help me create an expression. Thanks

    I00020036: Loaded C:\Documents and Settings\ja03\Desktop\DSMProduct\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\IsConfig.ini into \DSM R11\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\IsConfig.ini .

    I00020036: Loaded C:\Documents and Settings\ja03\Desktop\DSMProduct\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\Setup.rul into \DSM R11\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\Setup.rul .

    E03020039: Unable to load C:\Documents and Settings\ja03\Desktop\DSMProduct\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\String1033.txt into \DSM R11\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\String1033.txt . Text file contains invalid characters .

    I00020036: Loaded C:\Documents and Settings\ja03\Desktop\DSMProduct\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\_ISUser1033.dll into \DSM R11\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\_ISUser1033.dll .

    I00020036: Loaded C:\Documents and Settings\ja03\Desktop\DSMProduct\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\_ISUser1033.rc into \DSM R11\projects\AIS\esounixagent\ServiceInstall\ServiceInstall\_ISUser1033.rc .

    I00020036: Loaded C:\Documents and Settings\ja03\Desktop\DSMProduct\projects\AIS\esounixagent\ServiceInstall\ServiceInstall.ism into \DSM R11\projects\AIS\esounixagent\ServiceInstall\ServiceInstall.ism .

    I00020036: Loaded C:\Documents and Settings\ja03\Desktop\DSMProduct\projects\AIS\esounixagent\ServiceInstall\ServiceInstall.sln into \DSM R11\projects\AIS\esounixagent\ServiceInstall\ServiceInstall.sln .

For Loop Output Errors
4 direct replies — Read more / Contribute
by Bama_Perl
on May 02, 2015 at 18:34
    I am fairly new to perl, and I am struggling with an output using a for loop and a nested for loop. I have an input file that looks like this:
    MCCC processed: unknown event at: Tue, 21 Oct 2014 13:39:56 CST station, mccc delay, std, cc coeff, cc std, pol , t0_times + , delay_times ZJ.APRL 0.5735 0.0270 0.8548 0.1060 0 APRL.BHZ 30 +1.0824 -1.0954 ZJ.BEBP 0.0431 0.0173 0.8982 0.0495 0 BEBP.BHZ 30 +0.6827 -1.2262 ZJ.DUBY -0.3951 0.0242 0.8635 0.0550 0 DUBY.BHZ 30 +0.9965 -1.9781 ZJ.FOOT 0.4722 0.0570 0.7965 0.0987 0 FOOT.BHZ 30 +1.2407 -1.3550 ZJ.GRAW -0.2962 0.0203 0.8875 0.0789 0 GRAW.BHZ 30 +0.5646 -1.4473 ZJ.KNYN 0.2933 0.0428 0.7879 0.1305 0 KNYN.BHZ 30 +1.3060 -1.5992 ZJ.LEON 0.5243 0.0235 0.8996 0.0634 0 LEON.BHZ 30 +0.4850 -0.5473 ZJ.MICH -0.1824 0.0165 0.8599 0.0713 0 MICH.BHZ 30 +0.1649 -0.9339 ZJ.RAPH 0.3076 0.0422 0.8096 0.0954 0 RAPH.BHZ 30 +0.4645 -0.7435 ZJ.RKST -0.7187 0.0401 0.8060 0.0827 0 RKST.BHZ 30 +0.3940 -1.6992 ZJ.SAMH -0.0702 0.0260 0.8930 0.0465 0 SAMH.BHZ 30 +1.0272 -1.6839 ZJ.SHRD -0.3952 0.0319 0.8343 0.0938 0 SHRD.BHZ 30 +0.8002 -1.7819 ZJ.SPLN -0.1563 0.0306 0.8653 0.0878 0 SPLN.BHZ 30 +0.5314 -1.2742 Mean_arrival_time: 299.4135 No weighting of equations. Window: 3.19 Inset: 1.28 Shift: 0.25 Variance: 0.03135 Coefficient: 0.85047 Sample rate: 40.000 Taper: 0.40 Phase: P PDE 2013 4 20 3 47 55.02 -5.002 152.111 65.3 0.0 5.6
    What the following script hopes to do is read the last line (with respective formatting) -- the line beginning with PDE. Let's call this the event information. Next, for EACH event, I need to read in the Station Name (Column 1, eg. ZJ.DONT), and the delay time (column 9). What I need to do, is output the Station name,the delay time and the number 1, 6 times across a column, and then the remaining stations (and respective delay times) will move to the next column, and if the remainder doesn't add up to six, pad the rest of the columns with zeroes, such as in this output:
    97 42121 2 27.38 0.00-12.544 0.000 166.815 0.000 29.90 0.00 83 7.6 0.0 +0 44 -1.45 1135 0.70 1105 -0.13 1547 0.04 1184 0.91 1168 -1.07 1 209 -1.28 1 41 -0.79 1163 0.72 1134 -0.59 1254 -0.95 1148 0.31 1 40 -0.24 1322 -0.68 1276 1.09 1338 0.11 1321 0.15 1132 -0.80 1 442 1.08 1107 -1.28 1 39 -0.09 1196 -0.04 1 31 -0.76 1 78 0.20 1 38 -1.43 1 80 0.45 1131 1.07 1164 0.19 1274 -0.29 1526 1.29 1 186 0.15 1108 0.45 1277 0.83 1 91 0.83 1554 0.45 1160 -0.30 1 225 0.33 1505 -0.11 1154 0.75 1204 -0.18 1228 0.94 1143 -0.60 1 243 -1.82 1229 0.18 1 93 -0.29 1247 -0.94 1227 -0.47 1 76 0.10 1 123 0.58 1 96 0.78 1 84 -0.03 1242 0.51 1182 -0.26 1244 0.37 1 232 -0.25 1246 0.70 1226 -0.22 1245 0.71 1189 1.05 1165 0.21 1 230 0.17 1444 -0.95 1272 0.51 1234 1.20 1 32 0.34 1 77 -1.90 1 150 0.34 1124 0.47 1157 -0.33 1 34 -0.58 1 28 -0.59 1199 -0.37 1 185 -0.58 1119 0.04 1490 0.03 1463 -0.06 1330 0.50 1255 -0.04 1 231 -0.17 1 30 0.16 1331 0.77 1523 -0.43 1191 0.58 1 0 0.00 0
    Where the first number represents the station number, the second number is the delay time (column 9) and the third column is just a 1. What I have thus far is below:
    open(TABLEA, "mcp_list"); @tablea = <TABLEA>; # Specify the correspoding output file open(OUT,">output_inversion"); for ($i = 0; $i < @tablea; $i++) { chomp ($tablea[$i]); ($mcpFile) = (split /\s+/,$tablea[$i])[0]; system("wc $mcpFile > crap"); open(TABLEB,'crap'); @tableb = <TABLEB>; chomp ($tableb[0]); ($count) = (split /\s+/,$tableb[0])[1]; $numObs = $count - 9; close(TABLEB); unlink('crap'); #print $mcpFile," ",$numObs,"\n"; $numLines = int($numObs/6); $remainder = $numObs - ($numLines*6); if ($numLines eq 0) { $numLines = $numLines + 1; } #print $numLines," ",$remainder,"\n"; # Now begin with the output file open(TABLEB, $mcpFile); @tableb = <TABLEB>; for ($j = 0; $j < @tableb; $j++) { chomp ($tableb[$j]); ($PDE,$year,$month,$day,$hour,$minute,$second,$eqlat,$eqlong,$ +eqdepth,$mag) = (split /\s+/,$tableb[$j])[0,1,2,3,4,5,6,7,8,9,11]; if ($PDE eq "PDE") { printf OUT "%2d%2d%2d%2d%2d %s %s%7.3f %s%8.3f %s%6.2f %s +%s %s %s \n", $year%100,$month,$day,$hour,$minute,$second,"0.00",$eql +at,"0.00",$eqlong,"0.00",$eqdepth,"0.00",$numObs,$mag, "0.00", "\n"; } for ($k = 0; $k < @tableb; $k++) { chomp ($tableb[$k]); ($netsta, $delay_time) = (split /\s+/,$tableb[$j])[1,9]; ($net, $sta) = (split /\./, $netsta)[0,1]; print $net, " ", $sta, "\n";
    In summary, I need to figure out a way to print (underneath each $mcpFile, the first and 9th column in that $mcpFile, along with the number "1", 6 times, with the remainders on the next line. It's long, I know, but I hope someone here can provide wisdom to send me on my way! Cheers.
LWP not working with HTTPS protocol (SOLVED)
5 direct replies — Read more / Contribute
by CountZero
on May 02, 2015 at 18:08
    Brothers & Sisters in Perl,

    I was writing some code to access my Flickr account, when I happened to have to access a resource over an SSL connection (https-protocol).

    Much to my surprise, LWP failed to access this resource and gave me a most strange error:

    Can't connect to api.flickr.com:443 Bad file descriptor at d:/Perl/perl/site/lib/LWP/Protocol/http.pm line + 41.
    The code I used was a very simple
    my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new( GET => 'https://api.flickr.com/services/ +rest'); my $response = $ua->request($req);
    Note that LWP makes the connection if I change 'https' by 'http'. Of course the webserver then refuses to return the requested resource as it requires an SSL connection.

    Note also that the error message refers to the LWP::Protocol::http-module, rather than the LWP::Protocol::https-module, as one should expect.

    Both modules are installed and up-to-date. Net::SSL is also installed and up-to-date (version 2.86).

    I am running Strawberry Perl 18.2 on a Windows 8.1 machine.

    Anyone any ideas how to make this work?

    Update: Adding the ssl_opts and protocols_allowed parameters solved the problem. Thanks all!

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Unpack or substr to create CSV?
6 direct replies — Read more / Contribute
by johnmck
on May 02, 2015 at 12:05
    I am a Perl neophyte when it comes to reading and parsing files. I am definitely in need of wisdom.

    I have a 3000 line text file that is comprised of 46 character lines:

    C4432882490H019000020150211ESL6690 0H2015PC
    C4833076550HC0P0000201412093J46651 0H2015DX
    C6033106980H057130020150323FRE7602 0H2015PC
    C663160140MT007015G20141124274847A MT2015PC

    Character 1 (or 0?) is a field, Characters 2-16 are a field, 17-30 another, and finally 31-46 are the last field. I want to read the file from /tmp (filename is always the same and always in the same place) and parse it into the fields (the counts never change) and then save it as a comma-delimited version.

    Since the fields never change length it would seem that unpack is a better choice than substr, is that correct?

    Is there a simple bit of code that someone might get me started with, please? Thank you!

hash dereferencing issue with perl 5.16.3
5 direct replies — Read more / Contribute
by KANAKADANDI
on May 02, 2015 at 10:49

    Hello Experts, We recently upgraded from Perl 5.8.8 to Perl 5.16.3. We started seeing strange issues with hash dereferencing issues. below script use to work fine in 5.8.8 and is causing issues in 5.16.3. we have a number scripts which use below kind of notation, is there anyway to make the below script to work by changing some perl generic settings rather than changing the scripts??

    #!/usr/bin/env perl use strict; use Data::Dumper; my %lHash; $lHash{'a'} = 1; $lHash{'b'} = 2; my $lHashRef = \%lHash; my $lValue1 = $lHashRef->{'a'}; print $lValue1."\n"; print Dumper($lHashRef); print ref($lHashRef) . "\n"; my $lValue = ${%{$lHashRef}}{'a'}; print $lValue."\n"; exit 0;

    it gives me...

    $ perl deref_test.pl 1 $VAR1 = { 'a' => 1, 'b' => 2 }; HASH Can't use string ("2/8") as a HASH ref while "strict refs" in use at d +eref_test.pl line 14.

    i tried using no strict "refs", then the warning goes away but it does not print the hash value. Below is the output from older version of Perl (5.8.8)

    $ perl perl_deref.pl 1 1 $VAR1 = { 'a' => 1, 'b' => 2 }; HASH 1

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
  • Outside of code tags, you may need to use entities for some characters:
            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.