Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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
multiple array processing
1 direct reply — Read more / Contribute
by f77coder
on Sep 02, 2014 at 21:49


    Am looking for some opinions on how to write these operations.

    @array1A=qw(a b c d); @array1B=qw(p z e t i u); @array_of_arrays=(@array1A, @array1B, @array2A,@array2B@arrayNA,@arra +yNB);

    sub arrays have different lengths have unique values and don't want to sort them. I want so sort the array_of_arrays based on following

    1. sort array based on number of elements in each from smallest to largest

    2. is it possible to sort the sub-arrays by name? then size?

    3. to do a random sort by name with (1A,2A) then (1B,2B,). Example


    the interesting part is the operations on the sub arrays, I do search of each sub array with a scalar value. the following loop has no sorting .

    while (<DATA>){ chomp; my @element = split; my $i1=shift @element; my $i2=shift @element; my $i3=shift @element; my $i4=shift @element; shift @element; shift @element; my $i7=shift @element; if(any{ $_ eq $i1} @array1A)) {print 'yes'; }elsif((any{ $_ eq $i1} @array1B)) {print 'no'; }elsif((any{ $_ eq $i2} @array2A)) {print 'yes'; }elsif((any{ $_ eq $i2} @array2B)) {print 'no'; }else{print "WTF";} };

    I need the If loop rearranged based on the order of the array_of_arrays Is this possible?

    All help is appreciated.

Create new pdf File
1 direct reply — Read more / Contribute
by eversuhoshin
on Sep 02, 2014 at 19:16

    Hello, I want to get rid of the first two pages in a pdf file so I can later convert it to an excel file. Here is the file

    I get an error message that the pdf is cross referenced from my code. Any other way to do it would be appreciated. Thank you so much.

    use strict; use warnings; use CAM::PDF; use PDF::API2; my $pdfone = CAM::PDF->new('C:/Documents/2014q1.pdf') || die "$CAM::PD +F::errstr\n";; my $pdftwo = PDF::API2->open('C:/Documents/2014q1_new.pdf') ; my $font = $pdftwo->corefont('Helvetica-Bold'); for my $pagenum (3.. $pdfone->numPages() ) { my $text = $pdfone->getPageText($pagenum) or next; my $page = $pdftwo->page(); # add a new page my $pdf_text = $page->text(); $pdf_text->font($font,12); my @lines = split("\n",$text); my ($x,$y) = (50,700); for my $line (@lines) { $pdf_text->translate($x,$y); $pdf_text->text($line); $y = $y - 20; } } $pdftwo->saveas('C:/Documents/2014q1_new.pdf');
MD5 and SHA Hashes
1 direct reply — Read more / Contribute
by Green One
on Sep 02, 2014 at 19:08
    I am trying to find a script that will parse through a group of flat file and pull out only the MD5 and/or SHA hashes and dump them one line at a time to another file. The end results can be over 16,000 lines.
Parsing Motorola S-Rec file
3 direct replies — Read more / Contribute
by gri6507
on Sep 02, 2014 at 17:14
    Fellow monks,

    I was rather surprised that my CPAN search for a module to parse Motorola S-record files (see Wikipedia for details) returned no useful results. Also, my Google search for this utility returned only one useful result (some Renesas PDF with poorly written Perl code). Surely I can't be the only one who needs to convert S-rec files into other formats. I can, of course, write my own, but I didn't want to reinvent the proverbial wheel. Does anyone have any references?

using Exporter in <Perl> block in mod_perl
No replies — Read more | Post response
by cLive ;-)
on Sep 02, 2014 at 16:23

    I haven't done any old school mod_perl in a while, and I'm not sure if this is a coding style issue, global var issue or if I need to set something before implementing this or what...

    There's a config module that loads configuration values and uses Exporter to expose $config in @EXPORT_OK. When I try to import it in a <Perl> block though, it throws an error. Here's the code (snipped)

    <Perl> use XXX::Config qw($config); </Perl>

    And the error I get is:

    error (22)Invalid argument: <Perl>: Invalid command 'config', 
    perhaps mis-spelled or defined by a module not included in the server

    I'm not really sure where this issue is coming from. Anybody have any pointers?


Fixing Kwalitee
1 direct reply — Read more / Contribute
by skx
on Sep 02, 2014 at 16:17

    I maintain a couple of modules on CPAN, and having recently started exploring the facilities available there I've come across the "kwalitee" score.

    Using the TinyDNS::Reader module as an example I see that the score listing shows an error:

    META.yml does not conform to any recognised META.yml Spec.

    My generated tarball, made with "perl Makefile.PL ; make dist" contains what looks to me like a valid META.yml file, so I'm a little confused as the the problem.

    Can a kind soul help me understand my problem? (The module comes from a github repository, if that is useful.)

Perl/TK NoteBook Q.
1 direct reply — Read more / Contribute
by ronene1972
on Sep 02, 2014 at 10:22

    Hi Guys! I'm new here & also new to Perl world.. Actually I'm an Electronics Engineer (ASIC Design) & I started playing with Perl/TK as I need to build a GUI tool for running my simulations.. Please take a look at the below: I've built a little MainWindow in which I can open several Tabs NoteBooks). Each Tab is differently named by the user and should consist its own Button. The problem is that I do not know how to "tie" the Buttons into the Page (Tab) they displayed on i.e. If you open a Tab labeled "1" & click Buttons "Option1" & "Option3", and now you open a new Tab labeled "2" - you'll see that Buttons "Option1" & "Option3" are activated... How do I grant a unique variable to Buttons of each Page? Best Regards, Ronen

    #!/usr/local/bin/perl use Tk; use Tk::NoteBook; use Tk::DialogBox; use Tk::LabEntry; $mw = MainWindow->new(); $mw->title("TEST"); $mw->geometry( "400x300"); $mw->configure(-menu => my $menubar = $mw->Menu); my $file = $menubar->cascade(-label => '~File'); $file->command( -label => 'New Test', -underline => 0, -command => \&new_test, ); $file->separator; # -------------------------------------------------------------------- +--------------------------- ## Create the notebook and fill the whole window $nb = $mw->NoteBook()->pack(-expand => 1, -fill => 'both'); MainLoop; # Open new Single Test Run Tab subroutine sub new_test { # Popup for selecting name for new Tab $db = $mw->DialogBox(-title => "Select Test Name", -buttons => ["OK", +"Cancel"]); $db->add('LabEntry', -textvariable => \$test_name, -width => 20, -label => 'Test Label:', -labelPack => [-side => 'left'])->pa +ck; $db->Show( ); # Create the notebook and fill the whole window # Page 1 on the Notebook, with buttons on that page $p1 = $nb->add($test_name, -label => $test_name); $lbl_0 = $p1 -> Label(-text=>"$test_name Options:") -> place(-x => 0, +-y => 100); $rdb_1 = $p1 -> Checkbutton(-text=>"Option1", -variable=>\$option1) -> + place(-x => 100, -y => 100); $rdb_2 = $p1 -> Checkbutton(-text=>"Option2", -variable=>\$option2) -> + place(-x => 100, -y => 120); $rdb_3 = $p1 -> Checkbutton(-text=>"Option3", -variable=>\$option3) -> + place(-x => 100, -y => 140); }
Problem while installing module from CPAN on windows.
3 direct replies — Read more / Contribute
by kiranp
on Sep 02, 2014 at 08:06
    Hi Monks,

    my first ever CPAN moduel released on site but wondering what went wrong that i am not able to install it on windows and i couldn't find build.log file on my system since i am using padre editor for the same.

    I have released my module *Speedometer* in CPAN and soon after i tried to install it on windows.

    but getting following error. what could be the reason. thanks.

    now i am getting following error

    --> Working on

    Fetching ... OK

    Configuring Speedometer-1.06/ ... OK

    ==> Found dependencies: Benchmark

    skipping R/RJ/RJBS/perl-5.20.0.tar.gz

    Building and testing Speedometer-1.06 ... FAIL ! Installing failed. See \.cpanm\build.log for details.


Point me in the right direction with OO inheritance and static variables
6 direct replies — Read more / Contribute
by Amblikai
on Sep 02, 2014 at 05:54

    Hi Monks, still getting to grips with OO Perl. I know there are modules out there like Moose et al, but i' writing classic perl Objects as a learning experience.

    I'm not sure where to begin with inheritance though. I was hoping someone could point me in the right direction?

    I'd like to have a class which has some static attributes. For example if i create an object it will be created with hardcoded attributes without me passing them as arguments to the "new" constructor. How do i go about that?

    Then i have a second class which i will be creating objects which can only have attributes which are found in the previous class. I don't know how to get the second class to know it can only have those attributes and to only allow the object to be called with those attributes having variables assigned to them.

    Any help greatly appreciated.

Improving the Nested For Loop
6 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 02, 2014 at 00:28

    Hi Monks,

    I need your wisdom to improve my "nested for loop". I am trying to do correlation calculation for about 50,000 categories with each category has 100 values. Here is the snippet of my code:

    #!/tools/bin/perl use strict; use warnings; ## Variables and Data Structures my $count = 1; my @probesArray my %probes; my $size = 100; ## Reading the file open my $FILE, "data.txt" or die "ERROR: cannot read file $!\n"; while (my $line = <$FILE>){ chomp $line; my @line = split('\t',$line); $probes{$line[0]} = [@line[1 .. $#line]]; ## value of the hash as an a +rray $probesArray[$count] = $line[0]; ## correlation between 1-2 or 2-1 wil +l be same so using calculating only once can be done thru this array $count++; } close($FILE); ## Frankly speaking reading of the file takes less than +3 sec with 50,000 categories each having 100 values ## Correlation Calculation for(my $i = 0; $i <= $count-1; $i++){ for(my $j = $i+1; $j <= $count; $j++){ my @probe1 = @{$probes{$probesArray[$i]}}; ## Array of data my @probe2 = @{$probes{$probesArray[$j]}}; my $cor = correlation(\@probe1, \@probe2, \$size); ## correlat +ion is the subroutine $calProbes{$probesArray[$i]."-".$probesArray[$j]} = $cor; # print $count,"\t",$probesArray[$i]."-".$probesArray[$j],"\t", +$cor,"\n"; $count++; } } ## Subroutines sub mean { my ($arr1, $arr2, $size) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; my $mu_x = sum(@arr1) / $$size; my $mu_y = sum(@arr2) / $$size; return($mu_x,$mu_y); } ## Sum of Squared Deviations to the mean sub ss { my ($arr1, $arr2, $mean_x,$mean_y) = @_; my @arr1 = @$arr1; my @arr2 = @$arr2; my ($ssxx, $ssxy, $ssyy) = (0) x 3; ## looping over all the samples for(my $i = 0; $i <= scalar(@arr1)-1; $i++){ $ssxx = $ssxx + ($arr1[$i] - $mean_x)**2; $ssxy = $ssxy + ($arr1[$i] - $mean_x)*($arr2[$i] - $mean_y) ; $ssyy = $ssyy + ($arr2[$i] - $mean_y)**2; } return ($ssxx, $ssxy, $ssyy); } ## Pearson Correlation Coefficient sub correlation { my ($arr1, $arr2, $size) = @_; my ($mean_x,$mean_y) = mean($arr1, $arr2, $size); my ($ssxx, $ssxy, $ssyy) = ss($arr1, $arr2, $mean_x, $mean_y); my $cor = $ssxy/sqrt($ssxx*$ssyy); return($cor); }

    Correlation Calculation is taking a lot of time. I mean calculating correlation between category1 vs all (i.e. 50000) is taking around 40 sec. Now, if that is the speed then, it will take more than 12 days to calculate the correlation coefficient for all the 50k categories. Nested for loop is taking a lot of time. Is there a way to decrease the run time or am I doing something wrong.

    Any hints or advice would be highly appreciated. Thanks!

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

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2014-09-03 05:08 GMT
    Find Nodes?
      Voting Booth?

      My favorite cookbook is:

      Results (35 votes), past polls