For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > October 2006 > Reading data from a textfile









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reading data from a textfile
edjacob1@gmail.com

2006-10-03, 6:55 pm

I am trying to print data from a text file into an HTML table. The
data in the text file is organized like:
dog||cat||rabbit
monkey||lion||tiger
....
When I run the script, the Javascript command comes out correctly for
the first two items in each line, but not the last one. In the last
one, the Javascript is broken up into 2 lines so it doesn't work. If I
organize my data like dog||cat||rabbit|| with pipes at the end, it
works, but I'd like to not use them if possible. Why doesn't it work
without them? Am I misusing the split command? This is the script:

###Print data from textfile into table
open (DAT, $datafile) || die("Could not open file!");
while ($i < scalar(@lines))
{
print "<tr>";
$line = <DAT>;
@line = split(/\|\|/, $line);
foreach $line (@line)
{
$a = "<p onclick\=\"FP_openNewWindow('330'\,'300'\, false\,
false\, false\, false\, true\, true\,''\,
/*href*/'http\://images.google.co.jp/images?q\=";
$b = "')\">";
$c = "</p>";
print "<td bgcolor\=\"#C0C0C0\">";
print "$a$line$b$line$c";
print "</td>";
}
$i++;
print "</tr>";
}
By the way, the script is running at: http://www.amapro.jp/cgi/index.htm

Paul Lalli

2006-10-03, 6:55 pm

edjacob1@gmail.com wrote:
> I am trying to print data from a text file into an HTML table. The
> data in the text file is organized like:
> dog||cat||rabbit
> monkey||lion||tiger
> ...
> When I run the script, the Javascript command comes out correctly for
> the first two items in each line, but not the last one. In the last
> one, the Javascript is broken up into 2 lines so it doesn't work. If I
> organize my data like dog||cat||rabbit|| with pipes at the end, it
> works, but I'd like to not use them if possible. Why doesn't it work
> without them? Am I misusing the split command?


No. Your split is fine.

> This is the script:
>
> ###Print data from textfile into table
> open (DAT, $datafile) || die("Could not open file!");
> while ($i < scalar(@lines))


Where did @lines come from? Whenever possible, please try to post a
short but COMPLETE script.

> {
> print "<tr>";
> $line = <DAT>;


When you read a line from a file, the *entire* line is read, including
the terminating newline.

> @line = split(/\|\|/, $line);


So now that newline is attached to the last field of the array @line.
Then you go to print it out, and of course the newline is printed with
it.

Change your
$line = <DAT>;
to
chomp ($line = <DAT> );

> foreach $line (@line)
> {
> $a = "<p onclick\=\"FP_openNewWindow('330'\,'300'\, false\,
> false\, false\, false\, true\, true\,''\,
> /*href*/'http\://images.google.co.jp/images?q\=";
> $b = "')\">";


My god. What are all those slashes for? The *only* ones that are
doing anything are the two before each of the internal " characters.
For the love of my eyes, please rewrite this!

my $a = "<p onclick = \"FP_openNewWindow('330', '300', false, false,
false, false, true, true, '',
/*href*/'http://images.google.co.jp/images?=";
my $b = ")\">";

Paul Lalli

edjacob1@gmail.com

2006-10-04, 7:55 am

> Change your
> $line = <DAT>;
> to
> chomp ($line = <DAT> );
>

Thanks so much. I've been trying to figure this out for days.
[color=darkred]
> My god. What are all those slashes for? The *only* ones that are
> doing anything are the two before each of the internal " characters.
> For the love of my eyes, please rewrite this!


I had no idea. I read somewhere that periods, commas, equals signs and
quotations all need slashes in front of them. Thanks again.

Paul Lalli

2006-10-04, 6:55 pm

edjacob1@gmail.com wrote:

>
> I had no idea. I read somewhere that periods, commas, equals signs and
> quotations all need slashes in front of them. Thanks again.


If you ever find that "somewhere" again, please burn it.

In a double-quoted string, the only things that need to be slashed are
the $, @, and \ itself.
In a regular expression, the "dirty dozen" need to be backslashed: ^ $
| ( ) [ { . * + ? \
In any string, whichever character you choose for your delimiter (which
could be ' or ", or if using q or qq, any non-alphanumeric) must be
backslashed.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com