in reply to Re: Printing data from a file in various ways
in thread Printing data from a file in various ways

This is exactly what I am looking for; it works like a charm. :-D Now, the question is: How did you learn this type of effiecient coding? I have looked for Perl tutorials (I only started using it this summer when I setup my webserver) and all of them are the same old "how do Perl variables work" and such. Perhaps it is a matter of experience... I can at least understand what this code is doing now that I see it, I just have to learn how to write it.

Actually, I do have three questions:
  1. When $dispmode equals 3 (or display:1 and coordinate:1), it doesn't show the coordinates as an ordered pair like I would like it to. How could I fix this?(sub { print "<tr><td>" . join("</td><td>",split /,/, $_[0], 8) . "</td></tr>\n" })
  2. How would I need to change the code if I wanted to add another display option, such as filtering by name?
  3. If you have time, how would you like to review the code I use to get the data from an HTML page and parse it? (would be much appreciated, I know you might have a whole lot of better things to do :) )
Thank you very much for all of your help!

Replies are listed 'Best First'.
Re^3: Printing data from a file in various ways
by graff (Chancellor) on Oct 14, 2005 at 12:50 UTC
    How did you learn this type of effiecient coding?

    It's basically the principle of "informed, constructive laziness": you know what needs to be done, you figure out the minimum number of steps required to get it done, and you structure the program in such a way that you never write a given chunk of code (e.g. an "if" condition or quoted string or any multi-step procedure) more than once. For me, a key step is to document (in coherent, human language) what the code is going to do first, then write the code according to the documentation.

    When $dispmode equals 3 ..., it doesn't show the coordinates as an ordered pair

    I gather you mean it's not putting parens around the "x,y" in the last column of the table. If it's really important to get the parens in there, I guess I'd elaborate that sub a little:

    sub { my @cels = split( /,/, $_[0], 8 ); $cels[7] = '('. $cels[7] .')'; print '<tr><td>'. join('</td><td>',@cels) ."</td></tr>\n" }
    How would I need to change the code if I wanted to add another display option, such as filtering by name?

    You could filter entries as you read from the file, by altering the grep condition that I suggested for skipping blank lines; e.g. if you add a cgi param like "$name_pattern" (which the user can set to a string or leave blank), you can say:

    my $match = ( $name_pattern =~ /^(\w+)$/ ) ? $1 : "\\S": my @data = grep /$match/i, <FILE>;
    (updated to fix typo in sub snippet; also switched to using a regex capture in the second snippet, in accordance with the standard idiom for untainting data)