Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] Perl splitting help


On Sat, 22 Jul 2000, Scott Howard wrote:

> You need to quote the | character.
> 
> You can also simplify the rest of your code a bit too :
> 
> open PLAYLIST, "/tmp/playlist.txt" ||
>           die "Cannot open file for reading\!";
> while(<PLAYLIST>) {
>         ($name, $number) = split /\|/;
> 	print "  <tr>\n";
> 	print "   <td>$name</td>\n";
> 	print "   <td>$number</td>\n";
> 	print "  </tr>\n";
> }
> close PLAYLIST;

You also probably want to chomp your input to remove the new line and
output your table start and end. You can also simplify it more assuming
STDIO.

print "<table>\n";
while (<>) {
	chomp;
	print '<tr><td>';
	print join '</td><td>', split /\|/;
	print "</td></tr>\n";
}
print "</table>\n";

The split then join works well for any number of fields. You can also use
a replace of | to </td><td> like

print "<table>\n";
while (<>) {
	chomp;
	s/\|/<\/td><td>/g;
	print "<tr><td>$_</td></tr>\n";
}
print "</table>\n";

In Perl there is always more than one way to do it. Language of the Gods.

Rodos

-- 
rodos@xxxxxxxxxxx | Real programmers don't work from 9 to 5. If any real
Camion Technology | programmers are around at 9am it's because they were
+61 2 9873 5105   | up all night.                                 [Anon]