Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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
flip flop operator and if statement
2 direct replies — Read more / Contribute
by iThunder
on Sep 19, 2014 at 22:32
    Hello, Can someone please help that how value of p is coming out as 1 through 17. My understanding is p should be equal to $. (line number) and it should be printed from 3 to 19 rather 1 to 17. Thanks in advance! #!/usr/bin/perl ##use warnings; while(<>){ if($p=3..1) { print "p is $p\n"; } } When i run the same code on a file that has fifteen lines, it returns following p is 1 p is 2 p is 3 p is 4 p is 5 p is 6 p is 7 p is 8 p is 9 p is 10 p is 11 p is 12 p is 13 p is 14 p is 15 p is 16 p is 17 Also what is difference when i have $p=3..1 vs ($p=3)..1 in if statement above
How to convert negative and positive floating points to binary and vice versa
No replies — Read more | Post response
by thanos1983
on Sep 19, 2014 at 22:32

    Hello Monks,

    I have managed to convert positive and negative decimal numbers to binary and vice versa.

    Sample of code:

    I am trying to figure out how to convert positive and negative floating points. For example if I had a number -0.000008 how could I convert the number in binary and back to floating.

    I have read on the pack documentation that: F A Perl internal floating-point value (NV) in native format

    When I am modifying on my code the "c" to "F" I get the following error:

    So I assume I am going terribly wrong some where. What I am missing?

    Thank you in advance for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Faster indexing an array
5 direct replies — Read more / Contribute
by wollmers
on Sep 19, 2014 at 16:48

    Dear monks,

    as this part takes ~15% of an fast alignment algorithm, it would be nice to have a faster way.

    my $Y = [ qw( a b a) ]; my $YPos; my $index; for ( $index = 0 ; $index <= $#$Y ; $index++ ) { push ( @{ $YPos->{$Y->[$index]} }, $index ); } # now $YPos should contain my $result = { a => [ 0, 2 ], b => [ 1 ], };

    TIA (Thanks In Advance)

    Helmut Wollmersdorfer

sprintf to hash
3 direct replies — Read more / Contribute
by teamassociated
on Sep 19, 2014 at 14:45
    Been trying to figure out why this code is not storing my sprintf output. Please help! Thx!!!
    use strict; use warnings; $lsmapFH = new FileHandle "/usr/ios/cli/ioscli lsmap -vadapter $option +s[4] |" or die $!; while (<$lsmapFH>) { if (/no virtual device found/i) { warn "\n\nThis vhost $options[4] has no clients using me, tr +y again with a correct vhost #!\n\n"; die $!; } chomp; $vh = (split)[0] if /^vhost/i; $clid = (split)[2] if /^vhost/i; $cltid = sprintf "%d", $clid; } $lsmapHash{$vh} = { 'clientID' => $cltid, }; } use Data::Dumper; print Dumper \%lsmapHash; $VAR1 = { 'vhost10' => { 'clientID' => '0' } works fine in CLI: # perl -le '$clid=0x0000000a; $cltid= sprintf "%d", $clid; print $clti +d;' 10 __SAMPLE_OUTPUT__ from lsmap SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- vhost10 U9117.MMD.218A8B7-V1-C2 0x0000000b VTD vtscsi25 Status Available LUN 0x8100000000000000 Backing device l02duapXXXX_r1.58ff94782d7c325acc49a8dd9c04bb1e Physloc Mirrored N/A VTD vtscsi26 Status Available LUN 0x8200000000000000 Backing device l02duapofdev_b1.0ee86903d4951ce38a9b691b322ff1d2 Physloc Mirrored N/A
Stuttering Children
1 direct reply — Read more / Contribute
by cmv
on Sep 19, 2014 at 14:21
    Hi Monks-

    Consider the attached script.

    I believe I should get 2 lines of output from each of the 2 children. Running this under perl, v5.8.8 built for darwin works fine. However, when I run it under perl, v5.8.8 built on solaris, strange things happen:

    TRY1: CHILD1: My pid = 21727 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 21728 CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING... TRY2: CHILD1: My pid = 22218 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 22219 CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING...
    First, why do I sometimes get 4 lines from one “bad” kid??

    Second, why does the “bad” kid sometimes change?

    Any pointers very much appreciated!

    Thanks

    -Craig

    #!/opt/exp/bin/perl use strict; use warnings; foreach my $i (1..2) { _spawn($i); } sub _spawn { my $id = shift || die "Missing id\n"; my $pid = fork(); defined $pid or die "bad open (pipe/fork): $!\n"; # Have parent/child run their respective code... if ( $pid ) { # PARENT CODE... return; } else { # CHILD CODE... print "CHILD$id: My pid = $$\n" ; while(<DATA>) { print "CHILD$id: $_" }; exit } } __DATA__ 1: THIS IS YOUR PARENT SPEAKING... 2: THIS IS YOUR PARENT SPEAKING...
File re-formatting
3 direct replies — Read more / Contribute
by lma3j
on Sep 19, 2014 at 14:11
    I am new to Perl and am trying to determine if Perl is the right tool for the job. We currently are processing flat files we receive from vendors. The flat files have a series of about 7 or 8 fixed length record types. We are changing our processes, which means our flat file layout is changing. We may be adding fields, deleting fields, or changing the length of fields in the various record types. For the time being, we will need to continue processing the old file formats along with the new file formats until all vendors switch to the new format. I was thinking of using Perl to read the file coming and determine if it is the old format or the new format. If it is the old format, we would reformat each record type, adding the new fields, deleting the obsolete fields, and changing field lengths where needed. Does this seem like something for which Perl would be good to use? Thanks, Larry
Capture error from one-liner
4 direct replies — Read more / Contribute
by dirtdog
on Sep 19, 2014 at 12:04

    Hi Monks, Does anyone know how i would capture the return code from a perl one-liner executed from a korn shell script?

    perl -i.bak -pe 's/AJZ/AZZ/g if /^AB/' <filename> if [[ $? != 0 ]];then exit 1 fi

    if the one-liner fails for any reason I'd like to be able to exit with a failed status from the shell, but what i have doesn't work.

    any help would be greatly appreciated. thanks

perl : HTML:: Mason Print a perl hash inside HTML tags in a tabular format
3 direct replies — Read more / Contribute
by sravs448
on Sep 19, 2014 at 11:32
    I am trying to send a html email (report) about the values in a hash. But I couldnt print the hash values in a table format. I am using HTML::Mason to execute my perl commands (looping through a hash) and printing it in the end of report. But how can I execute my perl code to loop through hash inside html ?
    use HTML::Entities; use Mail::Sendmail; use MIME::Lite; use Term::ANSIColor; use Time::localtime; use HTML::Mason; my $ti = localtime; my ($day,$month,$year)=($ti->mday,$ti->fullmonth,$ti->year); # DEFINE A HASH %coins = ( "Quarter" , 25, "Dime" , 10, "Nickel", 5 ); $html = <<END_HTML; <table border='1'> <th>Keys</th><th>Values</th> <%perl> while (($key, $value) = each(%coins)){ </%perl> <tr><td>$key</td>; <td>$value</td></tr>; <%perl> } </%perl> </table>; END_HTML $msg = MIME::Lite->new( from => 'abc@mydomain.com', To =>'def@mydomain.com', Subject =>'Report', Type =>'multipart/related' ); $msg->attach( Type => 'text/html', Data => qq{ <body> <html>$html</html> </body> }, ); $msg->attach( Type => 'image/gif', Id => 'banner.jpg', Path => 'C:\Users\Pictures\banner.jpg', ); MIME::Lite -> send ('smtp','xxxs.xxxx.xxxxx.com' ); $msg -> send;
perl POST question in HTML
1 direct reply — Read more / Contribute
by justinzollar
on Sep 19, 2014 at 11:09

    I have an HTML/Perl form that allows a user to input data. The form where users input the data is called ManagementEnhancement.pl, and the form receiving the data is called ManagementEnhancementUpdate.pl. ManagementEnhancement.pl has POST. ManagementEnhancementUpdate.pl is where I am having an issue. On ManagementEnhancement.pl there is a text box called "txtName" and a person will enter in a name. On ManagementEnhancementUpdate.pl, I need a varaible to accept the value entered, and set it as the variable "$Name". Can someone please help me figure out what I need on the ManagementEnhancementUpdate.pl page in order to accept the data? Here are my use statements at the top of ManagementEnhancementUpdate.pl

    use CGI::Carp 'fatalsToBrowser'; use warnings; use Net::SMTP; use HTTP::Request::Common qw(POST); use LWP::UserAgent;
    right now, I'm using "get" and have this to extract the data out of the URL and assign it to variables.
    for $parm (@PARM) { chomp ($parm); $parm =~ s/[+]/ /g; $parm =~ s/%([0-9a-fA-F]{2})/pack ('c', hex ($1))/eg; if ($parm =~ /^[a-z0-9]+ *=/i) { ($parmname, $parmvalue) = split (/ *= */, $parm, 2); if ($parmname eq 'txtRequester') { $Reviewer = $parmvalue; } elsif ($parmname eq 'person') { $Person = $parmvalue; } elsif ($parmname eq 'team') { $Team = $parmvalue; } else { $TIME{$parmname} = $parmvalue; } } }

    any help would be GREATLY appreciated.

Parallel::ForkManager and CPU usage?
4 direct replies — Read more / Contribute
by janmartin
on Sep 19, 2014 at 11:00

    This is about Parallel::ForkManager on 64 bit strawberryperl.

    I need to process 1000s of images. Each totally independent of the others.

    Processing has CPU intensive parts, and parts that hardly use the CPU, but the GPU instead. The hardware has 8 cores, so I think of 8 processes? Each process to start multi-thread programs.

    Each image is of different size, and I know the processing time is different according to it. Therefore processes should run a different length.

    If a process is finished, a new process should start. To always have 8 processes.

    This should level the CPU usage after a few rounds due to the different time each process runs. But it doesn't.

    Looking at the Windows 8 Resource Monitor I see that all 8 processes run perfectly synchronized even after a long time.

    It looks like 8 process are started, then only after all 8 are finished 8 new processes are started?

    Code is straight from: http://search.cpan.org/~szabgab/Parallel-ForkManager-1.06/lib/Parallel/ForkManager.pm

    use Parallel::ForkManager; ... @links=( ["http://www.foo.bar/rulez.data","rulez_data.txt"], ["http://new.host/more_data.doc","more_data.doc"], ... ); ... # Max 30 processes for parallel download my $pm = Parallel::ForkManager->new(30); foreach my $linkarray (@links) { $pm->start and next; # do the fork my ($link,$fn) = @$linkarray; warn "Cannot get $fn from $link" if getstore($link,$fn) != RC_OK; $pm->finish; # do the exit in the child process } $pm->wait_all_children;

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.
  • 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 wandering the Monastery: (3)
    As of 2014-09-20 04:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (152 votes), past polls