Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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
Hash Multiple values for a key-Filtering unique values for a key in hash
6 direct replies — Read more / Contribute
by rahulme81
on May 22, 2017 at 23:11

    Hi, Below is the output of hash. I get multipe values for my keys and values appeared more than one, I need to have only unique values for a key.

    $HASH1 = { 'Alabama' => ['Andalusia', 'Anniston', 'Clanton', 'Eufaula', 'Auburn', + 'Bessemer', 'Eufaula', 'Auburn', 'Bessemer' ], 'California' => ['Barstow','Barstow'], 'Georgia' => ['Darien'], 'New York' => ['Amsterdam','Coney Island','Coney Island','Beacon','Bec +on'] };

    for e.g. for key "California" I get values 'Barstow','Barstow' (same value two times), I need to retain the only one value 'Barstow'

    what is a good way to check the existence for a key that has multiple values? and keep the value unique so my output looks

    $HASH1 = { 'Alabama' => ['Andalusia', 'Anniston', 'Clanton', 'Eufaula', 'Auburn', + 'Bessemer'], 'California' => ['Barstow'], 'Georgia' => ['Darien'], 'New York' => ['Amsterdam','Coney Island','Becon'] };


CGI hangs unless print
4 direct replies — Read more / Contribute
by janedoh
on May 22, 2017 at 18:21
    Dear all,

    I have a CGI script which does a lot of things such as logging in to our large company database to get information.
    There is a foreach loop that goes through a list and foreach item contacts the db, gets info, etc etc. This hangs unless I
    put a print "." in the foreach loop. I'm unclear why this would happen. Could it be buffering? When I flush the output
    it doesn't help. It needs a print statement or it will hang.
    Any ideas?

    Thanks for your help.
Imager::File::TIFF Dependencies
3 direct replies — Read more / Contribute
by BrentD
on May 22, 2017 at 17:31
    I have written a program that allows me to input some info, then calls Imager to output the info as Group4 Fax encoded TIF files. I am running this under Windows 10 64-Bit. The program runs fine if I use the .pl file, but if I use pp to build an .exe file and copy to another system, the file will not run. This is my current pp command call:
    pp -M Imager::Font::W32 -M Imager::File::TIFF -M -a cbxarrow.xbm -a ar +rowdownwin.xbm -u -o Program.exe
    This is what happens when I run the .exe file:
    E:\>Program.exe Can't load 'C:\Users\generic\AppData\Local\Temp\par-67656e65726963\cac +he-1fbb3c46710e1a62d9823bf27f4a9fe7a961c4bb\c6dc6748.xs.dll' for modu +le Imager::File::TIFF: load_file:The specified module could not be fo +und at C:/Strawberry/perl/lib/ line 193. at C:/Strawberry/perl/lib/ line 95. DynaLoader::croak("Can't load 'C:\\Users\\generic\\AppData\\Lo +cal\\Temp\\par-67656e657"...) called at C:/Strawberry/perl/lib/DynaLo line 193 DynaLoader::bootstrap("Imager::File::TIFF", 0.89) called at C: +/Strawberry/perl/vendor/lib/PAR/ line 120 PAR::Heavy::_bootstrap("Imager::File::TIFF", 0.89) called at C +:/Strawberry/perl/lib/ line 102 DynaLoader::bootstrap_inherit("Imager::File::TIFF", 0.89) call +ed at Imager/File/ line 10 Imager::File::TIFF::BEGIN() called at Imager/File/ line + 11 eval {...} called at Imager/File/ line 11 require Imager/File/ called at TCTC/ line 6 require TCTC/ called at script/ line 32 main::BEGIN() called at Imager/File/ line 11 eval {...} called at Imager/File/ line 11 require main called at C:/Strawberry/perl/vendor/lib/ li +ne 636 PAR::_run_member(Archive::Zip::ZipFileMember=HASH(0x37e33b0), +1) called at script/ line 26 require main called at C:/Strawberry/perl/vendor/lib/ li +ne 636 PAR::_run_member(Archive::Zip::ZipFileMember=HASH(0x37e3698)) +called at C:/Strawberry/perl/vendor/lib/ line 428 PAR::import("PAR") called at -e line 631 eval {...} called at -e line 41 __par_pl::BEGIN() called at Imager/File/ line 11 eval {...} called at Imager/File/ line 11 BEGIN failed--compilation aborted at Imager/File/ line 11. Compilation failed in require at TCTC/ line 6. Compilation failed in require at script/ line 32. BEGIN failed--compilation aborted at script/ line 32. E:\>
    Seems obvious that I have some sort of missing dependency, but I can't seem to find what that might be. Any ideas?
Trouble with fork()
2 direct replies — Read more / Contribute
by phonybone_monk
on May 22, 2017 at 16:55

    Hello, perlmonks,

    I am seeking advice on diagnosing a problem related to a Perl program (which I inherited) that makes use of fork(). The basic problem seems to be that some of the forked sub-processes immediately exit without doing any work. When those processes are reaped (with wait()), $? is set to 11, which I believe is the error code for "resource temporarily unavailable, try again". I am trying to determine which resource is unavailable. The environment is Linux/Centos 7

    The basic structure of this program is as follows:

    • - Initialization
    • - Read in a large file (~10Gb)
    • - Do some processing of the input file using other open-source software (bowtie and samtools), via calls to system(). This results in the writing of some intermediate files.
    • - Process the intermediate files in parallel. For each processor core unit (as reported by Sys::Info::Device::CPU, or by reading /proc/cpuinfo), my program calls fork(). But instead of then calling exec(), the child continues processing on its own.
    • - The parent process, after spawning all the children, enters a loop around a call to wait(). If wait() returns -1, the parent prints a message that includes the value of $?, and then exits (via Carp::confess).

    Here's a pseudo code outline (note: this won't actually re-create my problem, of course):

    # Initialization # Read input file, calls to system('bowtie2', ...) and system('samtool +s', ...) use strict; use warnings; use Carp qw(confess); use Sys::Info::Device::CPU; use Sys::Info::Constants qw( :device_cpu ); my $info = Sys::Info->new; my $cpu = $info->device( CPU => {} ); my $nthreads = $cpu->count; print "$nthreads threads\n"; while ($nthreads--) { my $pid = fork; confess ("Can't fork: $!") unless defined $pid; if (! $pid) { print "child process: $$\n"; # this line never gets executed +by some of the children # Do a bunch of processing using intermediate files as input, usin +g Bio::DB::Sam->get_features_by_location() to access intermediate fil +es exit 0; } } # parent continues here: my $pid = 0; my $ok = 1; while ($pid != -1) { $pid = wait; last if $pid == -1; my $err=$?; if ($err) { print("one of the children died: pid=$pid, err=$err\n"); $ok = 0; } } if (! $ok) { confess "some child processes died."; } print "all done, yay\n";

    In all of the processes, the line directly after the fork confess("Can't fork: $!") unless defined $fork; never gets executed. I infer that the fork itself did happen.

    But some of the processes, depending on the input file, are reaped immediately, with $err=11 (EAGAIN). I surmise I am running short of some resource, but which one? I have increased the limits for the following resources to values (by editing /etc/security/limits.conf, and then rebooting) that I believe should be more than sufficient, to no avail:

    • - number of open file handles
    • - number of allowed processes per user
    • - number of file locks allowed
    • - amount of lockable memory
    I've also increased swap space via swap files.

    Short or restructuring the code to use exec(), which I've considered, do any wise monks have suggestions as to:

    • - how to determine which missing resource is causing fork() to exit with $?=EAGAIN?
    • - any other useful areas to examine?

    My thanks for all answers, insights, and useful comments

What is correct way to reference?
6 direct replies — Read more / Contribute
by BradV
on May 22, 2017 at 13:13

    I'm writing some code to parse some xml files. One element could either have a single value or an array of values. I have:

    if ( ref($xmlcollection->{phone}->{callTo}) eq "ARRAY" ) { printf( "Call To:" ); foreach ($xmlcollection->{phone}->{callTo}) { print "$_\n"; } } else { printf( "Call To: %s\n", $xmlcollection->{phone}->{callTo}); }

    It just prints the array reference. If I add:

    printf( "%s\n",$xmlcollection->{phone}->{callTo}[0]); printf( "%s\n", $xmlcollection->{phone}->{callTo}[1]);

    it prints the correct values. The problem is I don't always know how many elements will be in the array. How do I correctly reference the array element of the hash in order to print out all of the values?

\b in Unicode regex
3 direct replies — Read more / Contribute
by Arik123
on May 22, 2017 at 02:45

    Hi Monks!

    I do

    $string =~ /$_/

    and it matches. I do

    $string =~ /\b$_\b/

    and it doesn't match, for the same values of $string and $_. I think it should match, since there's a hyphen or a dot after $_ in $string, which I think should match \b. Both $string and $_ are Unicode. Could it be that \b doesn't function for Unicode strings?

why is the size of notebooks area smaller than main window
2 direct replies — Read more / Contribute
by madhusudhan_s
on May 22, 2017 at 00:49


    I am trying to write a program which has many notebooks and i found a reference code from the web as below which is exactly what i want. When i make the main window size 870x1000 i notice that the area where the notebooks are present is smaller than the main window. How should i modify this code such that the notebooks are as big as the main window?
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::NoteBook; our $mw = MainWindow->new; $mw->geometry("870x1000"); my $canvas = $mw->Scrolled('Canvas',-bg=>'red')->pack; our $nb = $canvas->NoteBook; ### add 20 tabs to notebook for ( 1 .. 20 ){ my $title = "Untitled ($_)"; $nb->add( $_, -label => $title ); } ### embed the notebook widget into the canvas my $nb_in_canvas = $canvas->createWindow( 0,0,-window => $nb,-anchor = +> 'nw' ); $canvas->update; ### the whole notebook is larger than what can be initially displayed ### so we'll have to sroll it later on in the program $canvas->configure(-scrollregion => [0,0,$nb->reqwidth,$nb->reqheight] +); MainLoop;
scrape a webpage
2 direct replies — Read more / Contribute
by backyardbill
on May 21, 2017 at 23:26
    # ============ this is the HTML snipit from Firebug ================== +======== <td class="fex_standardblack_font_small" bgcolor="white"> <input id="chkEquipment" name="chkEquipment" value="72" type="checkbox +"> 4WD/AWD </td> # ====== this is the perl code snipit trying to access/extract the dat +a print "\n $node\n"; my $B = $tree->findvalue( $node ); $B = trim($B); print "\n $B \n"; # ============ this is what prints ================ /html/body/div/div[4]/div[2]/div[2]/form/div[3]/div/table[2]/tbody/t +r/td[3]/div/table/tbody/tr[1] 4WD/AWD # ============ this is my question ================ # # how do I access/extract the 'value="72"' data?
syntax error
3 direct replies — Read more / Contribute
by keiththirgood
on May 21, 2017 at 11:39

    I am new to all coding and I have no idea what is wrong here. I have a simple form I'm trying to send cleaned up results from to an email address. This is the error message I received:

    syntax error at feedback.cgi line 33, near "print MESSAGE "From"

    syntax error at feedback.cgi line 35, near "n\"

    syntax error at feedback.cgi line 79, near "}"

    Execution of feedback.cgi aborted due to compilation errors.

    This is the code near each of those lines:

    32 print MESSAGE "To: keith\\n; # Don't forget to escape this @ symbol!

    33 print MESSAGE "From: " . $FORM{name} . ", reader\n";

    34 print MESSAGE "Reply-to: " . $FORM{email} . "(" . $FORM{name} . ")\n";

    35 print MESSAGE "Subject: Feedback from $FORM{name} \n\n";

    36 print MESSAGE "$FORM{name} wrote:\n\n";

    76 EndHTML


    78 exit(0);

    79 }

Experimental push on scalar now forbidden
3 direct replies — Read more / Contribute
by gsd4me
on May 21, 2017 at 04:35

    Sorry to interrupt your tonsure creating dear monks but some advice please. It appears that Perl has adapted itself and has thrown our system's operation, established during a small routine change to our code.

    The code creates several data types from definitions in a package, and stores them as a series of joined records - so one record holds the data pointer of another etc. (I have simplified the code to show you below) (It was so much easier to use address manipulations in 'C' but Perl's list manipulation was one of the original reasons why we went with Perl over 'C'). The trouble is that the code that was working before has now seemingly been banned as being anti- the Perl Spanish Inquisition's latest thinking on purity. Obviously a code rewrite/redesign is *not* desired at this stage so is there a better/newer/easier way of simply pushing the element onto the list held by the 'root' structure?

    The error being given is:

    Experimental push on scalar is now forbidden at C:\crap\ line 40, near "$rohan1;"

    Experimental push on scalar is now forbidden at C:\Users\Alan\Documents\crap\ line 46, near "$rohan4;" Many thanks - ADB

    Package/data sources package rohanType1; sub new { my $self = {}; # Discard the class name shift; $self->{'field11'} = undef; bless ($self); return $self; } return 1; package rohanType2; sub new { my $self = {}; shift; $self->{'field21'} = 0; bless ($self); return $self; } return 1; package rohanType3; sub new { my $self = {}; shift; # Pointer to rohanType1 $self->{'field31'} = undef; # Pointer to rohanType2 $self->{'field32'} = undef; bless ($self); return $self; } return 1; # Now the data structures used globally to store information used by t +he whole analysis, which holds # data types as defined above package rohanData; sub new { my $self = (); shift; $self->{'rohan1'} = undef; $self->{'rohan2'} = (); bless ($self); return $self; } return 1; ======================= Code my $rohanRoot; my $rohan1; my $rohan2; my $rohan3; my $rohan4; $rohanRoot = rohanData->new(); $rohan1 = rohanType1->new(); $rohan2 = rohanType2->new(); $rohan1->{'field11'} = $rohan2; $rohanRoot->{'rohan1'} = $rohan1; # my debug helper to see if add +resses are correct - not part of code push $rohanRoot->{'rohan2'}, $rohan1; # Interpreter/debugger objec +ts to this ... $rohan3 = rohanType1->new(); $rohan4 = rohanType2->new(); $rohan3->{'field11'} = $rohan4; push $rohanRoot->{'rohan2'}, $rohan3; # ... and this

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!
  • 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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (8)
    As of 2017-05-25 11:40 GMT
    Find Nodes?
      Voting Booth?