tevoit has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I have been given a perl script to quickly extract rows from large excel files. It digresses from the scope of what I am working on as it is just a method for extracting data, but I would really like to know how it works. I have limited time to get to grips with it on my own.

Could someone please help me understand this script? It extracts a date, a weather station number and then a particular time.

#!/usr/bin/perl use strict; use warnings; my ($record, $date, $outfile, $station, $sstation, $linecnt, $pcntg, @ +values); # creating variables for user specification of date and station print "Processing \"$ARGV[0]\"...\n"; $date = $ARGV[2]; $station = $ARGV[1]; # create output file named after date being investigated $outfile = $date.".txt"; $outfile =~ s/ /-/; # counting number of lines print "For Date: $date and Station: $station\n\n"; $linecnt = `wc -l < $ARGV[0]`; open (INFILE, $ARGV[0]); open (OUTFILE, ">>", $outfile); while (<INFILE>) { $pcntg = int (($. / $linecnt ) * 100) ; print "$pcntg %\r"; chomp(); $record = $_; @values = split (',',$record); $sstation = $values[5]; $sstation =~ s/ //g; if ($values[0] eq $date && $sstation eq $station ) { print OUTFILE $record."\n"; # print "xx".$values[5]."xx\n"; print "Record written to $outfile...\n"; last; } } print "\nFinished\n"; close (INFILE); close (OUTFILE);

Best wishes

Replies are listed 'Best First'.
Re: Completely New To Perl - Need Help Commenting Script
by toolic (Bishop) on Jul 25, 2013 at 19:41 UTC
    Do you have the "Function Nodelet" available (its on the right side of my screen)? If so, it shows you a link to all the Perl built in functions which are in your code. The docs are only a click away! If not, you can enable it with Nodelet Settings from your home node.

    Docs are also available at your prompt:

    perldoc -f chomp

    Tip #2 from the Basic debugging checklist: add print's to your code and run it to see values of variables.

      Thank you for your constructive comments. I just don't have time to learn this from scratch right now; the 'Function Nodelet' is what I need. Best wishes

Re: Completely New To Perl - Need Help Commenting Script
by Jim (Curate) on Jul 25, 2013 at 18:31 UTC
    I have been given a perl script to quickly extract rows from large excel files.

    It's not a well-written Perl script in my opinion. A CSV file is not an "Excel file" just as a text file is not a Notepad file. Pre-counting lines in a large CSV file using a system call to wc -l just to display a silly percentage progress meter is dopey and wasteful. Splitting CSV records using split() is a bad idea in the general case. If the data records are actually in a large Excel workbook to begin with and are being saved to a large CSV file just to run a Perl script on them, then it might be smarter to use DBI with ODBC::DBD and the Microsoft Excel Driver and simply query the Excel workbook directly using an SQL SELECT statement. But if that's the case, then maybe the data should be in a database to begin with.

Re: Completely New To Perl - Need Help Commenting Script
by Anonymous Monk on Jul 25, 2013 at 17:02 UTC
    $linecnt = `wc -l < $ARGV[0]`;

    what is this?

      Non-portable equivalent of
      open my $FH, '<', $ARGV[0] or die $!; 1 while <$FH>; $line_count = $.; seek $FH, $. = 0, 0;
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Completely New To Perl - Need Help Commenting Script
by Laurent_R (Canon) on Jul 25, 2013 at 18:57 UTC

    I have limited time

    So do I. Sorry.

      So why waste your time commenting?

        Well, I am coming here and on other Perl forums mainly to try to help others and that does take quite some of my time (but I also learn a lot from reading other people responses, so that the time spent is not wasted in my view), but here, I had the feeling the OP (you) said: "I have no time for that, can you guys do it for me." OK, maybe I overreacted and misinterpreted your comment, but I was probably only trying to say that there are more efficient ways to ask politely and efficiently for help. You don't like it and are probably the one who gave me a down vote for it, fine, do as you like, I don't really care. I neither upvoted nor downvoted your posts (I actually almost never downvote any post, I think I must have done 2 or 3 times maximum ever since I have been coming on this site, and the first time actually by error), and, if you ask another question tomorrow, I will just try to answer it if I can, just as if nothing had happened.

        Though I concede Laurent_R's remark could be read as mere sniping (for lack of an explanation that the Monastery is not code-a-matic), my immediate reaction was that it was actually constructive because it might encourage the OP -- yeah, you, tevoit -- to consider what the node asked for: namely, free consultation by the Monks for the OP's $work. If the idea that that's not welcome here sinks in, you might undertake to learn rather than merely say 'gimme!'

        (I should not be surprised to learn you, tevoit, applied much the same logic to your arguably 'snippy' reply.)

        If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.