Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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
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
No replies — Read more | Post response
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
4 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?
Perl Menus
3 direct replies — Read more / Contribute
by PilotinControl
on Sep 30, 2014 at 16:19

    Hello Fellow Monks,
    I have a flat file filled with several fields and a script that can view all the fields in a formatted order. What I would like to do is be able to select a row by using the arrow keys and when I press enter I can view that row in a formatted view and be able to edit each field. I have looked at the Term::Menus module as it has the select option I am searching for. Any examples out there I can view that has what I'm searching for? Also if any perl programmer out there is looking for some part time work please private message me. Thanks!

using sub routines and solving an issue i have with passing variables
3 direct replies — Read more / Contribute
by james28909
on Sep 30, 2014 at 15:47
    Ok let say i have 4 sub routines like this:

    my $dataFile = $ARGV[0]; tear_down($dataFile); sub tear_down{ my ($dataFile) = @_; open my $file, '<', $dataFile || die "$!"; my count = 3; for ( my $i = 1 ; $i <= $count ; $i++ ) { seek to bytes in file; read x bytes in file; } } sub parse_file_1{ my ($file) = @_; stuff.... } sub parse_file_2{ my ($file) = @_; stuff... } sub parse_file_3{ my ($file) = @_; stuff... }
    just say for example the first subroutine extracted 3 parts of a file. how would i pass those to the other three subroutines? Do i just put sub name like sub parse_file_1($var) and pass it like that? or maybe i am missing something totally lol. i have been able to use subs in the past but i would like to understand the logic a little better. any insight would be wonderful
Using Perl instead of awk!
6 direct replies — Read more / Contribute
by Ragged Robin
on Sep 30, 2014 at 14:50

    Hey guys, I just need an example of how to express something in Perl the way I know how to do it with awk.

    Ideally I would like to do something like this:

    my $pSWP = `awk '/something/ { print $3 }' /dir/file`;

    But the value of $pSWP appears to be null when I can confirm that the awk command should return a value when ran on the system. Apparently doing something like this is kind of a no-no and I can do the same thing in Perl but I'm not sure what all is available to do this in only Perl :(

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: (9)
    As of 2014-10-01 20:47 GMT
    Find Nodes?
      Voting Booth?

      What is your favourite meta-syntactic variable name?

      Results (38 votes), past polls