Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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
Prima scrollbar?
No replies — Read more | Post response
by over2sd
on Oct 01, 2014 at 23:24

    So, I've been looking into Prima as a replacement for Gtk2, and I'm liking it pretty well, but I'm having a terrible time trying to figure out how to replace Gtk2::ScrolledWindow.

    I've looked at the Prima perl source on Github, searched and search with my favorite search engine, and done some experimentation, but I haven't been able to figure it out.

    Is there a primer somewhere on adding scrollbars to a widget (and making it clip), or am I just not understanding one of the builtin widgets that will Just Work for this (and if so, how do I call it, because my trial and error on that has not yielded anything but vague error messages)?

    Any help will be greatly appreciated.

    If desired, I can post the code for my latest failed attempt, but I didn't want to waste time/space here if the case was that I need to use one of the built-ins.

Trying to use Win32::OLE PasteExcelTable method
1 direct reply — Read more / Contribute
by aaronbaum2000
on Oct 01, 2014 at 19:45

    Hello, I am writing a script to copy a range of cells from an Excel spreadsheet to a Word document. I want to keep the formatting of the range of Excel cells when I paste them into the word document. A simple copy and paste does not give me what I want (it seems to reformat the pasted cells in the word document to RTF and loses the cell/table structure). There is a method shown in the Word help that is called PasteExcelTable that I think may be my ticket, but I am having no luck using it through Win32::OLE. The code is shown below.

    $range = $doc->Paragraphs($doc->Paragraphs->Count)->Range; $range->PasteExcelTable({'LinkedToExcel'} => 0, {'WordFormatting' => 0 +}, {'RTF' => 0}); #Gives me "Invalid number of parameter" #$range->PasteExcelTable({'LinkedToExcel' => 0, 'WordFormatting' = +> 0, 'RTF' => 0}); #Gives me "Command not available"

    I'm confused about why in one instance above, it seems to find the method, but says it doesn't have the correct number of parameter, while in the other instance, it says the command is not available? I'm a total rookie on coding in any language, so be gentle... I can post more code if desired, but you'll really see the hack job I'm calling a script. Thanks in advance.

Empty strings after split /(\W)/
4 direct replies — Read more / Contribute
by wollmers
on Oct 01, 2014 at 16:29

    Is there an explanation, why split() behaves like this:

    # a simple tokenizer $ perl -e 'print join("|",split(/(\W)/,"Hello, World! ?")),"\n";' Hello|,|| |World|!|| ||? # get rid of the empty string tokens $ perl -e 'print join("|",grep {$_}split(/(\W)/,"Hello, World! ?")),"\ +n";' Hello|,| |World|!| |? # or use plain regex $ perl -e 'print join("|","Hello, World! ?"=~/(\W|\w+)/g),"\n";' Hello|,| |World|!| |?


    Helmut Wollmersdorfer

Regular Expression to Extract Anything from Colon Delimited String
6 direct replies — Read more / Contribute
by GuiPerl
on Oct 01, 2014 at 15:03
    I have put together some code to parse a colon delimited data file. The regular expression I have built traps some of the colon delimited values. In any case, I would like some pointers with the regexp pattern. An all purpose matching of anything in-between the colon delimited string would be ideal in addition to matching strings that contain fewer data values as can be seen below:
    foreach (<DATA>) { if($_ =~ m/(\d{1,2})?\:?(\w\d)?\:?(\b\w..*\b)?\:?(.*|N\/A)?\:?(\d{1,2} +.+)?\:?(\d{2}\s?GREEN|RED|XX)?\:?(.*)?\:?(.*)?\:?(\bsquare\b)?/) { #if($_ =~ $_ =~ m/(\d{1,2})\:?(\w\d)?\:?(\b\w..*\b)?\:?(.*|N\/A)?\|\|? +(\d{1,2}.+)?\:?(\d{2}\s?GREEN|RED|XX)?\:?(.*)?\:?(.*)?\:?(\bYELLOW\b) +?/) { if (defined $1) { $count=$1; } else { $count="nothing"; } if (defined $2) { #code $grade=$2; } else { $grade="nothing"; } if (defined $3) { #code $pos=$3; } else { $pos="nothing"; } if (defined $4) { #code $name=$4; } else { $name="nothing"; } if (defined $5) { #code $country=$5; } else { $country="nothing"; } if (defined $6) { #code $date=$6; } else { $date="nothing"; } if (defined $7) { #code $age=$7; } else { $age="nothing"; } if (defined $8) { #code $vacant=$8; } else { $vacant="nothing"; } if (defined $9) { #code $square=$9; } else { $count="nothing"; } #print "We have a match!\n"; print join " ",$count,$grade,$pos,$name,$date,$country,$age,$vacant,"\ +n"; } } __DATA__ 1:D2:DIRECTOR:D. Green:4/15/1953:61 XX:UNITED KINGDOM OF GREAT BRITAIN + AND NORTHERN IRELAND:::: 1:D1:DEPUTY DIRECTOR:D. Green::6/20/1964:50:TUNISIA REPUBLIC OF:::: 1:P5:SENIOR POLICY OFFICER:D. Green::7/7/1954:60 GREEN:UNITED KINGDOM +OF GREAT BRITAIN AND NORTHERN IRELAND:::: 9:P5:SENIOR ECONOMIST:D. Green::7/23/1958:56:UNITED KINGDOM OF GREAT B +RITAIN AND NORTHERN IRELAND:::: D. Green::10/29/1953:60 GREEN:PERU REPUBLIC OF:*::: D. Green::10/26/1955:58:SPAIN KINGDOM OF:*::: D. Green::5/15/1967:47:FRENCH REPUBLIC:::: D. Green:g:12/6/1954:59:FIJI REPUBLIC OF:::: D. Green::6/8/1967:47:UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRE +LAND:::: D. Green::9/16/1960:54:UNITED STATES OF AMERICA:::: N/A::Vacant:UNASSIGNED::YELLOW::
    Output from above:
    nothing D2 DIRECTOR:D. Green:4/15/1953:61 XX:UNITED KINGDOM OF GREAT B +RITAIN AND NORTHERN IRELAND ::: nothing nothing nothing D1 DEPUTY DIRECTOR:D. Green::6/20/1964:50:TUNISIA REPUBLIC OF +::: nothing nothing nothing P5 SENIOR POLICY OFFICER:D. Green::7/7/1954:60 GREEN:UNITED KI +NGDOM OF GREAT BRITAIN AND NORTHERN IRELAND ::: nothing nothing nothing P5 SENIOR ECONOMIST:D. Green::7/23/1958:56:UNITED KINGDOM OF G +REAT BRITAIN AND NORTHERN IRELAND ::: nothing nothing nothing nothing D. Green::10/29/1953:60 GREEN:PERU REPUBLIC OF *::: no +thing nothing nothing nothing D. Green::10/26/1955:58:SPAIN KINGDOM OF *::: nothing +nothing nothing nothing D. Green::5/15/1967:47:FRENCH REPUBLIC ::: nothing not +hing nothing nothing D. Green:g:12/6/1954:59:FIJI REPUBLIC OF ::: nothing n +othing nothing nothing D. Green::6/8/1967:47:UNITED KINGDOM OF GREAT BRITAIN +AND NORTHERN IRELAND ::: nothing nothing nothing nothing D. Green::9/16/1960:54:UNITED STATES OF AMERICA ::: no +thing nothing nothing nothing N/A::Vacant:UNASSIGNED::YELLOW : nothing nothing nothing nothing nothing nothing nothing
    Many thanks
Error retrieving Security Information (SACL) of certain registry keys
1 direct reply — Read more / Contribute
by dt667
on Oct 01, 2014 at 14:11

    Can anyone help me understand why certain Windows registry keys return an error when trying Win32::Security::Raw::GetNamedSecurityInfo is called? The below code was written to try and flesh out why my Win32::Security::NamedObject of type SE_Registry_Key is throwing the error 'GetNamedSecurityInfo: The system cannot find the file specified. at line 8.'

    For my system, 'MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Console' throws a different error and works as expected in my larger program that this snippet was pulled from.

    MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\CorruptedFileRecovery throws the error in question.

    Both registry keys have the same permissions as far as I can tell.

    use Win32::Security::ACL; use Win32::Security::Raw; my $objectType = "SE_REGISTRY_KEY"; my($psidOwner, $psidGroup, $pDacl, $pSacl, $pSecurityDescriptor) = Win32::Security::Raw::GetNamedSecurityInfo("MACHINE\\SOFTWARE\ +\Microsoft\\Windows NT\\CurrentVersion\\Console", $objectType, 'SACL_ +SECURITY_INFORMATION'); if ($pSacl) { my($AceCount, $AclBytesInUse, $AclBytesFree) = Win32::Security::Ra +w::GetAclInformation($pSacl, 'AclSizeInformation'); $sacl = "Win32::Security::ACL::$objectType"->new(Win32::Security:: +Raw::CopyMemory_Read($pSacl, $AclBytesInUse)); } else { $sacl = "Win32::Security::ACL::$objectType"->new(undef); } use Data::Dumper; print Dumper($sacl);
Question on flock
2 direct replies — Read more / Contribute
by kprasanna_79
on Oct 01, 2014 at 14:06

    revered Monks

    I am using a cloned version of UNIX. And when i apply flock on a file in a network its not working fine. It allows another program to modify the file. Can you please help me to lock the file in better way.

Compare two arrays
5 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 01, 2014 at 12:10
    Hi Monks!
    I have two arrays, I need to find if the elements of the first array matchs any element on the second array. I am getting stuck once I have the account numbers into a hash:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @account = qw( 96329XY 7776fc 334457yg 2233oop 000001 8877666b); my @file = qw(8877666b.txt 2233oop.txt 334457yg.txt 7776fc.txt 96329X +Y_K.txt); my %seen; for my $acc (@account) { next unless $acc =~ /^\d+/; my $key = $acc; push @{$seen{$key}}, $acc; } #print Dumper %seen; for my $filename (@file) { next unless $filename =~ /^\d+/; my $key = $filename; ... }

    Thanks for looking!
perl: How to comapre and manipulate csv file in perl?
4 direct replies — Read more / Contribute
by Ankur_kuls
on Oct 01, 2014 at 07:55

    Hi friends. Below is the sample of my csv file

    Amount ChoiceId Allowed PID AmountId MultiplyingFactor + PF new ServiceTax ExtraTalkTime MRP Recharge Type FRC +/SRC 13 na 285 100 1.1236 81.75 0.02 0 92 Freebi +e(BA)+PID Normal 59 na 285 101 1.1236 8.31 0.07 0 10 Freebie +(BA)+PID Normal 13 na 289 100 1.1236 81.75 0.05 0 92 Freebi +e(BA)+PID Normal 2001 na 285 103 1.103 27.13 2.06 0 52 MA+Da +taPack+PID Normal 1572 na 285 104 na 0 na na na Blank MRP, re +st OK Normal 1574 na 285 105 na 0 na na na Blank MRP, re +st OK Normal 333300 na 285 106 1 0 0 0 3333 Freebie(BA)+ +MA+PID Normal 93 na 285 107 1.1236 100.53 0.11 0 114 Free +bie(BA)+PID+SA Normal 78 na 285 108 1.1236 8.12 0.1 0 10 Freebie( +BA)+PID Normal 79 na 285 109 1.1236 85.54 0.1 0 97 BA20 on +ly Normal 13 na 290 100 1.1236 81.75 0.11 0 92 Freebi +e(BA)+PID Normal

    now as per the requirement, if values of amount, amountId and MRP are same for particular rows then in that case i need to replace it by only one row in which value of AllowdPID and Service tax will take values from all the input the rows, separated by semi colon..(rest of the values we will not manipulate as they will be equal automatically)... for example as the amount, amountId and MRP values for 1st, 3rd and last rows are equal. our output will contain only one line in place of these three line like below.

    13 na 285;289;290 100 1.1236 81.75 0.02;0.05;0.11 + 0 92 Freebie(BA)+PID Normal

    how can I achieve it using perl. I tried someting using Text::CSV but can't share as it becomes a total mess :(...please help.

    below is my script

    #! /usr/bin/perl use strict; use warnings; use Text::CSV; my $basepath = "/home/eankuls/perl/vodafone/punjab"; my $IPfile = "Consolidated_output.csv"; my $OPfile = "Final_report.csv"; open (my $FH, "${basepath}/${IPfile}") or die "Can't open $IPfile file +: $!"; open (my $WFH, ">>", "${basepath}/${OPfile}") or die "Can't open $OPfi +le file: $!"; my $csv = Text::CSV->new({ sep_char => ",", binary => 1, auto_diag => 1 }); my $row = $csv->getline($FH); my $flag = 0; my $flag1 = 0; my (@row1, @row2); while (my $row = $csv->getline($FH) ) { my (@AllowedPID, @STax); if ($flag == 0) { @row1 = @{$row}; $flag++; push (@AllowedPID, $row1[2]); push (@STax, $row1[6]); next; } else { @row2 = @{$row}; } if ($row1[0] == $row2[0] and $row1[3] == $row2[3] and $row1[8] +== $row2[8]) { if ($flag1 == 0 ) { push (@AllowedPID, $row1[2]); push (@STax, $row1[6]); $flag1++; } push (@AllowedPID, $row2[2]); push (@STax, $row2[6]); next; } else { $row2[2] = join(";", @AllowedPID); $row2[6] = join(";", @STax); my $row_final = join (",", @row2); print $WFH $row_final; @row1 = @row2; next; } #print WFH @row2; $flag = 0; $flag1 = 0; }
Count in intervals
5 direct replies — Read more / Contribute
by rkk
on Sep 30, 2014 at 20:01


    My input files have following. I need to compute counts that fall in each interval mention in FILE2 using third column in FILE1.

    I am looking for ways other then using SQLITE. My FILE1 has more than 10 million lines so linear search is not a good option.


    so 10 0.05

    so 11 0.03

    so 25 0.15

    so 35 0.3

    so 36 0.25

    so 37 1

    ge 14 0.12

    ge 20 0.4


    so 10 20

    so 30 40

    ge 10 30

    sample output:

    so 0.08

    so 1.55

    ge 0.52

    Thanks in advance,


cpan install Unicode::Map8 fails
1 direct reply — Read more / Contribute
by DKMudrechenko
on Sep 30, 2014 at 17:27
    Greetings Mighty Monks, I am trying to install `Unicode::Map8` module using cpan on CentOS5 with 2.6.18-371.9.1.el5 kernel. So when I do cpan> install Unicode::Map8 as sudo I get the following result:
    Running install for module Unicode::Map8 Running make for G/GA/GAAS/Unicode-Map8-0.13.tar.gz Is already unwrapped into directory /root/.cpan/build/Unicode-Map8 +-0.13 Has already been processed within this session Running make test PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-MTest: +:Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib +/lib', 'blib/arch')" t/*.t t/callback....ok + t/compat......ok + t/map8........dubious + Test returned status 0 (wstat 11, 0xb) t/unistr......ok + Failed Test Stat Wstat Total Fail Failed List of Failed ------------------------------------------------------------------ +------------- t/map8.t 0 11 ?? ?? % ?? Failed 1/4 test scripts, 75.00% okay. 0/6 subtests failed, 100.00% + okay. make: *** [test_dynamic] Error 255 /usr/bin/make test -- NOT OK Running make install make test had returned bad status, won't install without force
    The perl version is v5.8.8. I tried this on my machine and on server that runs the same config. Any Idea what might be the reason?

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 studying the Monastery: (8)
    As of 2014-10-02 04:47 GMT
    Find Nodes?
      Voting Booth?

      What is your favourite meta-syntactic variable name?

      Results (49 votes), past polls