Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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
Unable to read excel columns more than 700
3 direct replies — Read more / Contribute
by doubledecker
on Sep 22, 2014 at 02:42

    Hi Monks

    I have an excel sheet with more than 1000 columns and trying to read data out of it. But, Spreadsheet::XLSX is not letting to read more than 700 columns. I have tried using other modules but the result is same. May I know what is the exact reason behind this?


Finding Minimum Value
5 direct replies — Read more / Contribute
by jimmy88
on Sep 21, 2014 at 19:03

    Please forgive me for my naivety but I'm having an issue that I'm sure one of the wise perl monks can quickly resolve: My script processes all of my input data as desired with the exception of minimum values for the input grades. What is the issue here and how can I ensure accurate output? Once again, sorry to bother by asking for help with something so seemingly simple. Your help is much appreciated.

    ***students.txt*** 122334:James,Lebron: 222223:Duncan,Tim: 244668:Bryant,Kobe: 355779:Durant,Kevin: ****************** ***grades.txt*** 122334 1 98 222223 1 86 244668 1 89 355779 1 90 122334 2 96 222223 2 88 244668 2 92 355779 2 96 122334 3 97 222223 3 96 244668 3 95 355779 3 94 122334 4 97 222223 4 96 244668 4 95 355779 4 94 122334 5 97 222223 5 96 244668 5 95 355779 5 94 **************** #!/usr/local/bin/perl #Assign class roster file to variable. $students = 'students.txt'; # Open class roster. open (NAMES, "<$students") || die "Couldn't open $students $!"; # Create and populate arrays. while (<NAMES>) { ($id,$name) = split(':',$_); $name{$id} = $name; # Monitor name length for purpose of print formatting. if (length($name)>$longestname) { $longestname = length($name); } # Creat group size variable for later use with class avera +ges. $groupsize = $.; } # Arrays are complete. Close file. close NAMES; # Assign grade spreadsheet to variable. $grades = 'grades.txt'; # Open grade spreadsheet. open (GRADES,"<$grades") || die "Couldn't open $grades $!"; # Create, populate and assign arrays. while (<GRADES>) { ($id,$exam,$grade) = split; $grade{$id,$exam} = $grade; # Monitor exam counter. if ($exam > $lastexam) { $lastexam = $exam; } } # Arrays are complete. Close file. close GRADES; # Create, format and print table headings. printf "%6s %-${longestname}s ", 'ID#','Name'; foreach $exam (1..$lastexam) { printf "%4d",$exam; } printf "%10s",'Total'; printf "%8s",'Avg'; printf "%8s",'Min'; printf "%8s\n\n",'Max'; # Define alphabetical sort subroutine. sub alpha { $name{$a} cmp $name{$b} } # Print formatted student data. foreach $id ( sort alpha keys(%name) ) { printf "%6d %-${longestname}s ", $id,$name{$id}; # Set total point counter to zero. $total = 0; foreach $exam (1..$lastexam) { printf "%4s",$grade{$id,$exam}; # Counter increment. $total += $grade{$id,$exam}; $examtot{$exam} += $grade{$id,$exam}; # Calculate minimum grades. if ($grade{$id,$exam} < $mingrade) { $mingrade = $grade{$id,$exam}; } # Calculate maximum grades. if ($grade{$id,$exam} > $maxgrade) { $maxgrade = $grade{$id,$exam}; } } # Print student's point total. printf "%10d",$total; # Print student's average. printf "%8d",$total / $exam; # Print student's minimum exam grade. printf "%8d", $mingrade; # Print student's maximum exam grade. printf "%8d\n", $maxgrade; } # Print heading for class averages. printf "\n%6s %${longestname}s ",'',"Average: "; # Calculate and print class averages. foreach $exam (1..$lastexam) { printf "%4d",$examtot{$exam} / $groupsize; } # Exit script. exit(0);
last update time of hardware clock (Windows and Linux) with the Server
1 direct reply — Read more / Contribute
by thanos1983
on Sep 21, 2014 at 17:49

    Hello Monks,

    I am looking for a function that can provide me the last update time of the hardware clock on Windows/Linux OS through Perl.

    Update: The last update time that the OS was synchronized with a time server.

    I have been looking over the web and I can not find something similar. I can find modules to play around with the hardware clock such as Test::MockTime or Time::Mock but they are not suitable from my request.

    I want a function that will reply back with the epoc seconds since the last update time. For example since last update time of hardware clock was 5.7 sec.

    Thank you in advance for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Why isn't this simple method to join elements of 2 arrays not working
3 direct replies — Read more / Contribute
by Axlex
on Sep 21, 2014 at 10:35
    Hello wise Monks; i am trying to concatenate the lines of 2 files and came up with this simple code
    my (@In, $In, @Out, $Out, $InFile, $OutFile); + # declare variables open $InFile, "<", "e:/File1.txt"; # open the exported +file in read mode open $OutFile, ">>", "e:/File2"; @In = <$InFile>; @Out = <$OutFile>; close $InFile; close $OutFile; for my $k ( 0 .. $#In ) { $Out[$k] = "$Out[$k],$In[$k]"; } open $OutFile, ">", "e:/File2.txt"; # open the outfile, + deleting all content (writemode) print $OutFile @Out; close $OutFile;

    but i am getting the error: "Use of uninitialized value within @Out in concatenation(.) or string at"

    I know, my programming skills are bad but please, i dont know any other way.

3 direct replies — Read more / Contribute
by anikng
on Sep 21, 2014 at 03:46
    Hellow Monks, I saw some posts similar to my following doubt. But i couldnt figure it out. So kindly help me (am not an expert). I have a front end html (with buttons) and a backend PERL cgi script. The code is correct and tested in another windows system. Now, i placed two files in www folder (under var) in Ubuntu 14.04. But when i clcik the button, instead of runing the cgi, script itself is displayed. Someone kindly help me what changes to be made (in a simple way). I also attach the contens of front end html and backend test.cgi

    Front end.html <html> <body> <h3> <center>TRANSCRIPTION FACTOR FAMILY</center></h3> <form action="/var/www/test.cgi" method="GET"> <input type="submit" value="ABC"> </form> </body> </html>


    #!/usr/bin/perl use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); $q=new CGI; print "Content-type: text/html\n\n"; @field=$q->param(); $name=$q->param("value"); print<<"OUTPUT"; <html> <head> <title> FAMILY SELECTION </title> </head> + <body> <img src="/var/www/bb.jpg" size=100"> <br><br><br><br><br> <center><table border=0> <tr><td> </td></tr> </table></center> </body> </html> OUTPUT
    Thanks, anikng
Extract table from a block of text
1 direct reply — Read more / Contribute
by reaper9187
on Sep 21, 2014 at 03:01
    Hello Monks, I have a text file which contains a lot of information, most of it unimportant. However, there is a block of text that I need to parse which contains information in a tabular format as shown below.
    INFO START STIME ETIME COLUMN3 COLUMN4 COLUMN5 aaaa1 bbb1 ccc1 ddd1 eee1 aaaa2 bbb2 ccc2 ddd2 eee2 aaaa3 bbb3 ccc3 ddd3 eee3 aaaa4 bbb4 ccc4 ddd4 eee4 END
    The sample output should be a table(hash maybe ?) that maps each element to its corresponding row as follows:
    aaaa1:bbb1 ccc1 aaaa1:bbb1 ddd1 aaaa1:bbb1 eee1 aaaa2:bbb2 ccc2 aaaa2:bbb2 ddd2 aaaa2:bbb2 eee2 aaaa2:bbb3 ccc3 aaaa2:bbb3 ddd3 aaaa2:bbb3 eee3
enumerating values for slopes
2 direct replies — Read more / Contribute
by Datz_cozee75
on Sep 20, 2014 at 23:46

    Hello monks,

    I'm in the middle of a window project, and it's the part where I'm supposed to make the field measurements to determine the slope of the sill. It's a tricky measurement with significant consequences, and I want to have a list of values to judge from. I typically do this type of thing in fortran, but this just isn't working for me in a variety of ways. There probably isn't anything wrong with it, it's just that I need to have more flexibility, so that I can see the values under differing assumptions. The fortran program I want to replace follows

    $ gfortran -Wall -Wextra -o out marvin1.f90 marvin1.f90:4.36: real :: rise, run, theta, deg_to_rad, rad, pi 1 Warning: Unused variable 'deg_to_rad' declared at (1) $ ./out pi is 3.14159274 rise is 0.421622515 $ cat marvin1.f90 program main implicit none real :: rise, run, theta, deg_to_rad, rad, pi run = 3.0 theta = 8.0 pi = 4.0*atan(1.0) print *,"pi is", pi rad = theta * pi / 180.0 rise = run * tan (rad) print *, "rise is ", rise end program main ! gfortran -Wall -Wextra -o out marvin1.f90 $

    The perl replacement will have a list of values for runs, a list of values for rises, and it will calculate all the permutations and list them in order. This is my modest achievement so far. The values are carefully chosen for this very task:

    $ perl slopes are 0 5 8 11 14 30 90 runs are 2.25 3.25 4 5 $ cat #!/usr/bin/perl -w use strict; use 5.010; my @slope = (0.0, 5.0, 8.0, 11.0, 14.0, 30.0, 90.0); say "slopes are @slope"; my @run = (2.25, 3.25, 4, 5); say "runs are @run"; __END__ $

    One of the reasons I usually revert to fortran is that I don't want to have to re-think getting numbers like pi right. So it is that I will have a math module for perl that will give me pi and degrees_to_radians at the end of this thread. I think there's a lot of ways to achieve these results and was fishing for methods and people's experience with the same material.

    My question then is: how do I solve this problem using perl, whilst generating a *robust* module that will never have me starting from scratch again like this (famous last words)?

How to compare the data of two files (.xml and .html) using perl(regex)?
4 direct replies — Read more / Contribute
by flora
on Sep 20, 2014 at 15:32
    I tried using regex by using foreach loop for .xml files as there are many .xml files and for .html file as it is only one file I just opened, read and closed the directory. But when it comes to searching for a particular pattern in both the files, the code doesn't enter the while/if loop. Can please someone help me.

    Original content restored above by GrandFather

    say the xml file contains the following data: <gname>abc</gname> <pname>xyz</pname> and the html file contains the following data:



    Here, I need to match both ABC and XYZ (case sensitive) from both the files(.xml and .html). The code I used is: open(F2,"<F2>"); my $xml_list1="(.*)\.html"; #here the data enclosed inside the parentheses also appears when printed. I want say the file name is "abc.html" so i want to keep "abc" as interchangeable, so that i dont need to write/modify the code if any filename other than abc.html occurs. close F2; #print $xml_list1."\n"; foreach my $f(@filenames) { #print $f."\n"; open(F1,"<F1>"); my $data=join("",<F1>); close F1; my $filename=substr($f,0,index($f,'.')); my $xml_list=$filename.".xml"; while($xml_list=~m//ig) #the code doesn't enter the while/if loop, seems that it finds some error in reading the filename $xml_list, but i tried using $xml_list1 too... but still the loop doesnt work. Need urgent help
Where does perl looks for perl skripts [Answered]
2 direct replies — Read more / Contribute
by BluePerl
on Sep 20, 2014 at 14:54

    I found, that perl only looks in
    * the actual directory, or
    * in the path which I provided for the script

    Is it possible to provide something like a PATH environment variable, which tells perl where to look for *.pl skripts?

    I created for testing purposes in the directory C:\MyDir:
    print "bar\n";

    And tested here:
    C:\MyDir>perl bar C:\MyDir>cd .. C:\>perl Can't open perl script "": No such file or directory C:\>perl -I C:\MyDir Can't open perl script "": No such file or directory C:\>set PATH=%PATH%;C:\MyDir C:\>perl Can't open perl script "": No such file or directory C:\>perl C:\MyDir\ bar

    As noted above, I would like to start by
    I carefully readed and found the switch -S, which includes the path environment variable for searching. So
    C:\>perl -S
Convert arrayref to AoH
3 direct replies — Read more / Contribute
by neilwatson
on Sep 20, 2014 at 09:31


    I have a default array of hashes. I replace the defaults, if they exist, with an arrayref. Is there a more efficient way to do this?

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Defaults my @t = ( { 'label' => 'kept', 'value' => '0', }, { 'label' => 'notkept', 'value' => '0', }, { 'label' => 'repaired', 'value' => '0', }, ); # Get this from another sub my $a = [ [ 'kept', '1', ], [ 'repaired', '3' ] ]; # Overwrite defaults with arrayref for my $i ( @{ $a } ) { for my $t ( @t ) { if ( $i->[0] eq $t->{label} ) { $t->{value} = $i->[1]; } } } warn Dumper( \$a ); warn Dumper( \@t );


    $VAR1 = \[ [ 'kept', '1' ], [ 'repaired', '3' ] ]; $VAR1 = [ { 'value' => '1', 'label' => 'kept' }, { 'value' => '0', 'label' => 'notkept' }, { 'label' => 'repaired', 'value' => '3' } ];

    Neil Watson

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: (9)
    As of 2014-09-22 10:38 GMT
    Find Nodes?
      Voting Booth?

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

      Results (188 votes), past polls