Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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
Spreadsheet::XSLX parsing workbook, but not retrieving values from cells
3 direct replies — Read more / Contribute
by divyahk
on Sep 17, 2014 at 06:35

    Hello, I am a newbie to Perl and have a problem with parsing an Excel file. I am using Spreadsheet::XLSX for reading data from an Excel file and extract each row of data, one row at a time. The code (partial) is as follows:

    use strict; use warnings; use Spreadsheet::Read; use Data::Dumper; my $excel = Spreadsheet::XLSX->new (‘xyz.xlsx'); if (!defined $excel) { die "Error: " . $excel->error() . "\n"; } print “Read with Spreadsheet::XSLX\n"; print Dumper($excel) . “\n";

    The output here shows that it’s reading the correct workbook as it can recognise the correct worksheet called “Paper.1”, and I think I am using the correct version of the Excel parser as it is indeed the .xlsx variety of Excel. Why are the cells not being read then? (As shown by cells =>undef)

    Read with Spreadsheet::XLSX : $VAR1 = bless( { 'Worksheet' => [ bless( { 'DefColWidth' => '8.43', 'MinCol' => 0, 'MaxRow' => 0, 'MinRow' => 0, 'path' => 'worksheets/sheet +1.xml', 'MaxCol' => 0, 'Name' => 'Paper.1', 'Cells' => undef }, 'Spreadsheet::ParseExcel:: +Worksheet' ) ], 'Flg1904' => 0, 'FmtClass' => bless( {}, 'Spreadsheet::XLSX::Fmt2007' + ), 'SheetCount' => 1 }, 'Spreadsheet::ParseExcel::Workbook' );
^CSIGINT handler "my_handler" not defined. Sigtrap is not able to find handler function.
2 direct replies — Read more / Contribute
by girijaj
on Sep 17, 2014 at 02:36
    Hi all, Anyone could explain why my sigtrap is not able to see handler function? I am getting "^CSIGINT handler "my_handler" not defined." error. use sigtrap 'handler' => \&my_handler, 'normal-signals'; and I have declared my_handler as: sub my_handler { die "Caught signal $! \n"; } Please help me I am not getting it what to do. thanks --girija
Multi file stream with IO::Compress::Zip
1 direct reply — Read more / Contribute
by Rodster001
on Sep 16, 2014 at 22:00

    Greetings! I am using IO::Compress::Zip but I cannot get the syntax quite right for what I am trying to do. Here is some pseudo code:

    use IO::Compress::Zip qw(zip $ZipError); my $zip = new IO::Compress::Zip \*STDOUT or print STDERR "IO::Compress::Zip failed: $ZipError\n"; foreach my $file (@files) { ... do some stuff to the file ... now add file to the archive } $zip->close;

    My problem: Using this the OO way I am not quite sure how to define the filename AND the contents of the file on each loop thus producing a stream to STDOUT with multiple files.


    I ended up using Archive::Zip::SimpleZip. This seems to work good and does in fact stream the zip file from the server as it is being processed.

    ## when using '-', Stream is set to 1 my $z = new Archive::Zip::SimpleZip '-', or print STDERR "Cannot create zip file: $SimpleZipError\n"; foreach my $file (@files) { ... do some stuff to the file my $fh = $z->openMember(Name => "<filename.ext>"); print $fh <stuff>; close($fh); } $z->close;
How do I delete lines matching a certain condition and make sure each line has the right prefix and suffix?
2 direct replies — Read more / Contribute
by cjff150
on Sep 16, 2014 at 19:17

    Hello, I'd appreciate any guidance on this -- I'm very new to PERL and can't crack this one. I have a pipe delimited file and the beginning of each line must start with | and end with || (no space in-between). Blank columns on a row should have a space inbetween the delimiters | |. I created the code below using regex to insert a space between the || delimiters anywhere a column might have blank data which is working ok.

    Here's an example, the code below changes the file from:




    To..(note spaces between delimiters for blank column data)

    |red| |blue|yellow| |black||

    |white|yellow| |teal| |purple||

    |red| |black|yellow| |teal||

    Here's the code

    #!C:\Perl64\bin\perl -w use strict; # Iteration 1, open myfile.txt open(FILE, "</temp/PERL-Samples/myfile.txt") || die "File not found"; my @lines = <FILE>; close(FILE); #This section uses an array and scans each line and does a #sequential search and replace for the following strings: #1. Finds any instance of || and changes it to | | #2. Finds any instance of | || and changes it to | | | #3. Finds any instance of || | and changes it to | | | #4. Finds any instance of | | at the end of a line and #changes it to || my @newlines; foreach(@lines) { $_ =~ s/\|\|/\| \|/g; $_ =~ s/\| \|\|/\| \|/g; $_ =~ s/\|\| \|/\| \|/g; $_ =~ s/\| \|$/\|\|/g; push(@newlines,$_); } #Push changes to MyFileNew.dat open(FILE, ">/temp/PERL-Samples/MyFileNew.dat") || die "File not found +"; print FILE @newlines; close(FILE);

    Another pre-existing condition this file has is there are several hundred lines where | and || exist on a line by themselves. Plus there are other lines not beginning with a | and ending with ||. For example:

    |red| |blue|yellow| |black


    |white|yellow| |teal| |purple||


    red| |black|yellow| |teal


    I'm not sure how to reliably and efficiently delete rows where || and | are the only characters in a line and also make sure each line is prefixed with | and suffixed with ||. Thanks in advance for reviewing this.

Perl regex \G /g and /gc
2 direct replies — Read more / Contribute
by tj_thompson
on Sep 16, 2014 at 19:03
    Hello monks,

    I've recently been doing a large amount of parsing and have wandered into a bit of a quandary related to regexes, \G, and /g.

    My recent code has been some state based lexing/parsing approaches for files that range from relatively simple to moderately complex formats. I've been using an approach of slurping the file in and using a number of regex tokens along with \G and /gc to parse through the resulting string. I've run into an issue with /g that I'd like to get advice on.

    Here's a simple example:

    use strict; use warnings; my $data =<<'END'; x = 10; y = 12; z = 100; END sub parse { my $data_ref = shift; my $rgx = qr/(\w+)\s*=\s*(\d+)\s*;\s*/; my @m = $$data_ref =~ /\G$rgx/gc; return \@m; } while (my @matches = @{parse(\$data)}) { while (my $var = shift @matches) { my $val = shift @matches; print "I got variable ($var) set to value ($val)\n"; } print "Trying next parse...\n"; }
    The output is:
    I got variable (x) set to value (10) I got variable (y) set to value (12) I got variable (z) set to value (100) Trying next parse...
    I would *like* the output to be:
    I got variable (x) set to value (10) Trying next parse... I got variable (y) set to value (12) Trying next parse... I got variable (z) set to value (100) Trying next parse...
    Ideally, I'd like to be able to handle these declarations one at a time. Get x, get 10, handle storing the data, then return to my string for parsing. However, in data formatted in a regular repeating fashion, the /g modifier results in multiple matches.

    /g seems to have two distinct functions: 1) ensure match position is retained after a match and 2) allow multiple matches to occur. I'd like to be able to retain the position of the match without the secondary effect of allowing multiple matches. We have the /gc modifier that allows retaining the match position after a failed match. My documentation reading suggests there is no similar modifier to only retain match position on a successful match outside of /g and its additional functionality. The pos function also only seems to work if /g is used.

    So my question. How do I retain both single token matching capability and /g's position tracking in the string? Note I'm particularly trying to avoid cutting the string itself up as string manipulation greatly slows the parsing.

Parse Chat Log to CSV
2 direct replies — Read more / Contribute
by Redfish76
on Sep 16, 2014 at 18:29

    I have a couple of chat logs I would like to parse to a csv. I am a noob to perl but think this should be fairly simple. Below is an example

    T 1310234540 19<24SomeUserName>19 This is user chat.
    T 1310234540 14<13AnotherUser>14 Some more chat messages.

    There are multiple lines in each log an I would like to parse it into a csv. Any idea's?

Text::CSV not able to execute insert query dynamically.
3 direct replies — Read more / Contribute
by Ankur_kuls
on Sep 16, 2014 at 16:15

    Hi, I have the below script which reads one csv file in a directory, connect to the SQLite database, and insert the data of csv files into the database tables..

    #! /usr/bin/perl use warnings; use strict; use DBI; use Text::CSV; use Data::Dumper; our $FH; open ($FH, "abc.csv") or die "can't open the fileo $!"; my $csv = Text::CSV->new({ sep_char => ";", binary => 1, auto_diag => 1 }); my $dbh; $dbh = DBI->connect('DBI:SQLite:vodafone.db') || die "$DBI::errstr\n"; my $columns = $csv->getline ($FH); # skip header my $count = $#{$columns}; my @array5; for (0..$count) { $array5[$_] = "'\$row->[$_]'"; } my $query = join (", ", @array5); print $query."\n"; while (my $row = $csv->getline($FH) ) { #$dbh->do("insert into VoucherMRPDataTable_Frt_TariffModel values ('$r +ow->[0]', '$row->[1]', '$row->[2]', '$row->[3]');"); $dbh->do("insert into VoucherMRPDataTable_Frt_TariffModel values ($que +ry);"); } close($FH); $dbh->disconnect;

    why i am using $query because I don't know how many columns this CSV file contains... Now when I execute the first insert query (commented out) I am getting the desired result ie field values of csv file. but when I am using the second insert query (which is my requirement) it is storing $row->[0], $row->1 etc values in database...please help...

Error while using POSIX strftime
2 direct replies — Read more / Contribute
by sravs448
on Sep 16, 2014 at 16:02

    I am getting an error using POSIX . strftime. I followed the guidelines mentioned in perldoc

    what is the mistake I did here

    use strict; use warnings; use POSIX qw(strftime); my $datestring = strftime "%B %d %Y", localtime; print("date - $datestring\n");

    Output message :
    Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = - 1, isdst = -1) at C:\Users\asdsa\Desktop\ line 4.

comparing sequence fasta headers
2 direct replies — Read more / Contribute
by Pasan
on Sep 16, 2014 at 13:56

    hi guys! I am a newbie to perl and I need help with finding list of fasta seuences which has specefic 3 letter code in the header. The question is explained below Scenario: I have a fasta file which contains list of sequences as follows (test.fa)

    >cel-let-7-5p MIMAT0000001 Caenorhabditis elegans let-7-5p


    >cfa-miR-761 MIMAT0009936 Canis familiaris miR-761


    >cfa-miR-764 MIMAT0009937 Canis familiaris miR-764


    >lus-miR167c MIMAT0027158 Linum usitatissimum miR167c


    I have set of organism codes in a separate file (codes.txt)




    what I want to do is search through test.fa for the codes and only print out the sequences which has that particular code in the header The example above contains only few sequences (not the entire file) So far I managed to create a hash and store the headers into keys and sequences into values. And I read through codes.txt and stored the codes in an array

    The problem is when I use 'if exists' function to find whether each code in array exists in the hash here is my code,

    #!/usr/bin/perl use warnings; use Bio::Perl; use Bio::Seq; use Bio::SeqIO; # Reading the first file and store it into a hash #the sequence header is stored in the hash key and sequence is stored +in the value my $FastaFile1 = Bio::SeqIO->new(-file => "test.fa", -format => 'fasta +', -alphabet => 'dna') or die "Failed to create SeqIO object from \n" +; my %fastaH1 =(); while( my $seqFile1 = $FastaFile1->next_seq() ) { unless (exists $fastaH1{$seqFile1->display_id."\t".$seqFile1->desc +}) { $fastaH1{$seqFile1->display_id."\t".$seqFile1->desc} = $seqFil +e1->seq; #key of the hash is fasta header (all line) and value is seq +uence. } } # printing the fasta headers print "stored fasta headers:\n"; foreach my $key (keys %fastaH1){ print "$key\n"; } # reading the codes.txt file and creating the array open my $file, '<', "codes.txt"; chomp(my @lines = <$file>); close $file; print "stored organism codes\n"; foreach (@lines) { print " $_\n"; } # if each code is found on the hash print match is found foreach my $line (@lines) { chomp $line; if( exists $fastaH1{$line} ){ print "match found\n"; } }

    The code is only printing "match found" if a match is found. But I need to write the sequences that has the 3 letter codes in a separate file. However, it doesn't seem to compare the codes and the fasta headers using exists function. Any help will be appreciated

Moscow time zone change in October 2014
3 direct replies — Read more / Contribute
by yanshuguang
on Sep 16, 2014 at 13:55

    Hi, Monks:

    According to, Moscow Time has been UTC+4 year-round since 27 March 2011, but will change to UTC+3 permanently on 25 October 2014.

    I am not an expert on Time::Zone module, could anyone have ideas what changes are needed in perl side to support this Moscow time zone change?

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 wandering the Monastery: (5)
    As of 2014-09-18 06:14 GMT
    Find Nodes?
      Voting Booth?

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

      Results (108 votes), past polls