Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Code request

by Anonymous Monk
on Feb 02, 2000 at 19:15 UTC ( [id://2734]=perlquestion: print w/replies, xml ) Need Help??

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

I've tried to learn perl, but I'm having a very tough time. I need a piece of code desperately soon and I have no idea where to start. Could someone help me out. I just need to take a text file and convert it into an .html file. The data in the file looks like this:

name 'Telecaster1' number '101' start_pos '7' finish_pos '1' laps_run '160' laps_led '72'
 qual_time '22.778' qual_speed '118.535' avg_time '34.188' avg_speed '78.975'
 best_time '22.164' best_speed '121.819' incidents '0' reason_out '0' 
name 'K_Hansen' number '112' start_pos '2' finish_pos '2' laps_run '160' laps_led '88'
 qual_time '22.625' qual_speed '119.337' avg_time '34.193' avg_speed '78.963'
 best_time '22.162' best_speed '121.830' incidents '0' reason_out '0'
name 'Irace_#22' number '109' start_pos '6' finish_pos '3' laps_run '160' laps_led '0'
 qual_time '22.763' qual_speed '118.613' avg_time '34.212' avg_speed '78.919'
 best_time '22.194' best_speed '121.654' incidents '0' reason_out '0' 

Note: there should be a total of three lines in the file format above, I split them up to make the entire page more readable, vroom
What I need is for an html table with the "name" field along the left side, and then the other fields across the top. The data for each field is inside the '' marks. Is this possible to do? If so could someone through it together real fast for me. I'm running perl on win98 machine. Thanks.

Replies are listed 'Best First'.
Re: Code request
by chromatic (Archbishop) on Feb 03, 2000 at 03:42 UTC
    Okay, this one was a little tricky. Here's the code:
    #!/usr/bin/perl -w use strict; my $file = shift || die "usage: $0 <text file>\n"; my @rows; my @temprows; my @header; local *INPUT; open (INPUT, $file) || die "$!\n"; my $first = 0; while(<INPUT>) { chomp; push @temprows, split /[ \']+/; my @row; while (@temprows) { if ($first == 0) { push @header, shift @temprows; push @row, shift @temprows; } else { shift @temprows; push @row, shift @temprows; shift @temprows; push @row, shift @temprows; } } $first = 1; push @rows, [ @row ]; } close INPUT; my $html = "<table><tr>\n"; foreach (@header) { $html .= "<td>$_</td>\n"; } foreach (@rows) { $html .= "<tr>"; foreach (@$_) { $html .= "<td>$_</td>"; } $html .= "</tr>\n"; } $html .= "</table>\n"; print $html;
    There are lots of ways to optimize this, but it works now. That'll be $250 for an hour of my time.
Re: Code request
by JP Sama (Hermit) on May 02, 2001 at 00:19 UTC
    You charge the full hour, right?

    because, if you took 1 hout to do this, your price should be lower *g***

    just kidding!!

    #!/jpsama/bin/perl -w
    $tks = `mount`;
    $jpsama = $! if $!;
    print $jpsama;
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2734]
This node hasn't been approved yet
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-24 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found