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

I'm simply trying to parse a text file and output in HTML...the code that I am currently using is similar to:
open(DATA,"myfile") or die "Can't open file"; print "<html><head><title>My page</title></head><body>"; print "<table>"; while(<DATA>) { my @elements = split('\|',$_); print "<tr>"; foreach my $el(@elements) { print "<td>$el</td>"; } print "</tr>"; } print "</table></body></html>";
However, my code doesn't appear to be doing anything at the foreach statement, thus the output is purely
<tr></tr>
Thoughts?

Replies are listed 'Best First'.
Re: string parsing
by mirod (Canon) on Apr 10, 2001 at 19:48 UTC

    DATA is a special filehandle (used to read data directly within the script after a __DATA__ line). Change the name of the filehandle to TOTO and it will work (and you will feel a little french).

      DATA is special in that it remains open if your script has an __END__ token or your module has a __DATA__ token. It isn't so special that you can't use it like a regular file handle if you want to.

      The above code should work unaltered (as others have already reported).

              - tye (but my friends call me "Tye")

        You are right! Shame on me! I would have sworn that I saw the script show the behaviour described in the question when using DATA. I guess it's a case where I convinced myself so much that this was the problem that I failed to notice an entire screen worth of <tr>'s and <tr>'s. Pretty impressive I'd say! And I guess I'll have to check things twice from now on!

        By the way, this is the beauty of Perl Monks, if a bad advice is given it is promptly corrected (without the flames you get on usenet!). Thanks.

Re: string parsing
by converter (Priest) on Apr 10, 2001 at 22:23 UTC

    Don't chase your tail when you run into a problem like this. The weak link in our code is the assumptions we make. More often than not, after we have enough experience, we can rely on our assumptions because we've learned which ones are safe, which ones aren't, and which ones to question first when something goes wrong.

    DATA is a special filehandle. I don't recall any documented warning against using it to open a regular file, but I don't think I'd assume that I could use it the way you're using it without running into a problem somewhere. When Perl has a special use for a word, I try not to use that word unless I want the Perl magic that comes with it.

    This may seem simple and/or obvious, but you're making the assumption that your code is reading input, but I don't see any proof of that. I would suggest adding a print statement at the top of the while() loop so you can confirm the input:

    print STDERR "bytes: ", length, "\n";

    If the reported input length for each line is greater than zero, you can assume you have data to work with. If the print statement is never executed, you know that you're not reading input, I suspect this is the case. I would suggest that you use a different filehandle, test again, then look for misspellings, make sure your input file isn't zero length, etc.

    Oh, and you are using warnings and strict, aren't you?

Re: string parsing
by Anonymous Monk on Apr 10, 2001 at 19:39 UTC
    The text file looks similar to this:
    5/5/2000 24|St. Louis Cardinals 1|4995|Fernando Vina|2B|4|0|0|0|0|0|0|.317|.397|.425 2|5602|Edgar Renteria|SS|4|0|1|0|1|0|1|.260|.321|.470 3|5151|Jim Edmonds|CF|3|1|2|1|5|1|0|.409|.538|.864 4|3866|Mark McGwire|1B|3|1|1|1|4|1|0|.323|.494|.903 5|4547|Ray Lankford|LF|4|0|0|0|0|0|3|.193|.297|.420 6|5046|Craig Paquette|3B|3|0|1|0|2|1|1|.264|.325|.486 7|6117|J.D. Drew|RF|4|0|1|0|1|0|1|.327|.462|.596 8|5205|Mike Matheny|C|3|0|0|0|0|0|0|.289|.375|.421 8|4506|Thomas Howard|PH|1|0|1|0|1|0|0|.280|.357|.760 8|5897|Eli Marrero|PR|0|0|0|0|0|0|0|.250|.343|.643 9|4379|Andy Benes|P|2|0|0|0|0|0|0|.167|.167|.167 9|3625|Eric Davis|PH|1|0|0|0|0|0|1|.281|.388|.491 9|4625|Heathcliff Slocumb|P|0|0|0|0|0|0|0|.000|.000|.000 9|4979|Mike Mohler|P|0|0|0|0|0|0|0|1.000|1.000|1.000 9|5880|Larry Sutton|PH|1|0|0|0|0|0|0|.000|.000|.000
      Run some tests on the @elements array. Check $#elementsto make sure it is splitting the lines properly. If it's not you have your problem - if it is try doing something like print "<td>$elements[0]</td>" and see if that works (place it outside the foreach loop).

      -Adam Stanley
      Nethosters, Inc.
Re: (Zigster) string parsing
by zigster (Hermit) on Apr 10, 2001 at 19:51 UTC
    Having just tested your code I get the following op, it looks like there may be something odd occuring. This is on a sparc with perl -v = 5.6.0 <html><head><title>My page</title></head><body>
    5/5/2000
    24St. Louis Cardinals
    14995Fernando Vina2B4000000.317.397.425
    25602Edgar RenteriaSS4010101.260.321.470
    35151Jim EdmondsCF3121510.409.538.864
    43866Mark McGwire1B3111410.323.494.903
    54547Ray LankfordLF4000003.193.297.420
    65046Craig Paquette3B3010211.264.325.486
    76117J.D. DrewRF4010101.327.462.596
    85205Mike MathenyC3000000.289.375.421
    84506Thomas HowardPH1010100.280.357.760
    85897Eli MarreroPR0000000.250.343.643
    94379Andy BenesP2000000.167.167.167
    93625Eric DavisPH1000001.281.388.491
    94625Heathcliff SlocumbP0000000.000.000.000
    94979Mike MohlerP00000001.0001.0001.000
    95880Larry SuttonPH1000000.000.000.000
    </body></html>
    --

    Zigster
      Same results as Zigster. (5.00503, PIII RH 6.2).

      So here's a suggestion with no justification, other than personal experience. Try substituting the single quotes in your split call with slashes (/). For some reason, I seem to remember encountering some unexplainable (or "unexplained to me") weirdness when tooling with the delimiters.

      Wish I knew why...
      Zigster, What elements in the code did you change to make it work? Also, what something strange could be occurring? Ideas? Thanks.
        That was what I was meaning, I changed nothing it worked as written. As to strange things. What platform are you on. Does the file have EOF and EOL markers in it?? What version of perl do you have .. Have you tried using perldb?
        --

        Zigster
Re: string parsing
by wardk (Deacon) on Apr 10, 2001 at 22:17 UTC

    could it be the single quotes in the split is causing the escape to not be acknowledged? so it's splitting literally on "backslash-pipe"?