newbie perl question - reading a file line by line 
Author Message
 newbie perl question - reading a file line by line

I'm a bit stumped on this one as I can't seem to get perl to check for an
end of line without giving me an error such as "value is not numeric in
eq".

I have a text file that I want to be turned in to html so the person
editing the text file doesn't have to learn html coding.

The text file format is:

H:Washer
D:Washing Machine
P: $50.00

I want to read the file line by line to a string so I can run it through
and build the html with the appropriate tags.

I'd rather be able to do this on the fly as I see no real advantage to
building an array to hold all the strings and then build my html.

Can someone point me to some docs on how to read files line by line? I did
check the FAQ but didn't find what I needed. If it is there, could someone
please point it out to me?

Thanks in advance!

--
Posted via CNET Help.com
http://www.*-*-*.com/



Wed, 18 Jun 1902 08:00:00 GMT  
 newbie perl question - reading a file line by line

Quote:

>I'm a bit stumped on this one as I can't seem to get perl to check for an
>end of line without giving me an error such as "value is not numeric in
>eq".

>I have a text file that I want to be turned in to html so the person
>editing the text file doesn't have to learn html coding.

>The text file format is:

>H:Washer
>D:Washing Machine
>P: $50.00

>I want to read the file line by line to a string so I can run it through
>and build the html with the appropriate tags.

my $file = "da_file";
my ${*filter*}y_huge_string;

open FILE, $file or die "Could not open $file: $!";
while (my $line = <FILE>)
{
  ${*filter*}y_huge_string .= $line;

Quote:
}

close FILE;

DoStuffWithString(${*filter*}y_huge_string);

Quote:
>Can someone point me to some docs on how to read files line by line? I did
>check the FAQ but didn't find what I needed. If it is there, could someone
>please point it out to me?

The Camel book, perhaps.

-Sami
--



Wed, 18 Jun 1902 08:00:00 GMT  
 newbie perl question - reading a file line by line

Quote:

> That sounds like you tried a C-like approach of looking for a
> terminating character.  Perl does it better than that.

Pascal actually, but close enough. :)

Thanks for the help, got more of it working now...

--
Posted via CNET Help.com
http://www.help.com/



Wed, 18 Jun 1902 08:00:00 GMT  
 newbie perl question - reading a file line by line
Dave schrob:

Quote:
> Can someone point me to some docs on how to read files line by line? I did
> check the FAQ but didn't find what I needed. If it is there, could someone
> please point it out to me?

open( IN, "<filename" );
my $a = <IN>;

gives you the first line in $a. You may use something like
while (<IN>) {
  # do something
  # for every loop you get the next line in $_

Quote:
}

Maybe it was so trivial you just didn't see it? ;-)

Regards
Christian

--
|~-_ /~~~~~ Free Linux Portal: http://www.linux-config.de ~~~~~\ _-~|
|  //       de.etc.schreiben.* - Usenet-Literatur im www:       \\  |
| //               http://www.usenet-autoren.de                  \\ |



Wed, 18 Jun 1902 08:00:00 GMT  
 newbie perl question - reading a file line by line
: open( IN, "<filename" );

In this group, it's not considered socially acceptable to post example
code that attempts to open a file but fails to check whether the open was
successful and give a diagnostic if it wasn't.  Too many learners have
come here asking for help with seemingly mysterious problems that turned
out to be the result of files not being in the expected place,
directories not having the right permissions, etc. after wasting hours of
their time for the experienced posters on this group to let this sort of
thing pass.  Perl novices should *not*, even unintentionally, be
encouraged to get in the very bad habit of blindly trusting everything
external to their programs.  You can't guarantee that Mr. Murphy won't
visit you, but failing to check external operations amounts to buying him
a first-class airline ticket and booking him a luxury suite in the
nearest hotel.

open (IN, "<filename") or die "Can't open 'filename' for input: $!";



Wed, 18 Jun 1902 08:00:00 GMT  
 newbie perl question - reading a file line by line

Quote:
> I'm a bit stumped on this one as I can't seem to get perl to check for an
> end of line without giving me an error such as "value is not numeric in
> eq".

> I have a text file that I want to be turned in to html so the person
> editing the text file doesn't have to learn html coding.

> The text file format is:

> H:Washer
> D:Washing Machine
> P: $50.00

> I want to read the file line by line to a string so I can run it through
> and build the html with the appropriate tags.

open(FH, "<$file") or die "Unable to open $file: $!\n";
while(<FH>) {
    chomp;
    # do some string manipulation here
Quote:
}

close(FH);

jay



Wed, 18 Jun 1902 08:00:00 GMT  
 newbie perl question - reading a file line by line
Eric Bohlman schrob:

Quote:
> In this group, it's not considered socially acceptable to post example
> code that attempts to open a file but fails to check whether the open was
> successful and give a diagnostic if it wasn't.

I understand, and I apologize. As you see if you open some other
postings from me, I usually do append an "or die $!", as I really
do not want to give a bad example.

Regards
Christian

--
|~-_ /~~~~~ Free Linux Portal: http://www.linux-config.de ~~~~~\ _-~|
|  //       de.etc.schreiben.* - Usenet-Literatur im www:       \\  |
| //               http://www.usenet-autoren.de                  \\ |



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. How do I read line by line of data from a file using perl

2. how to read a huge file line by line without loading it into memory

3. reading lines from one file and pulling matching lines in another

4. Line by line file reading

5. How to read a file line by line w/loop

6. Read remote text file line by line

7. reading a file line by line backwards

8. read last line without reading previous lines, how?

9. read line by line in perl

10. Writing lines to file twice or more (newbie question)

11. Deleting and or appending a single line in a text file - newbie question

12. how to read in line by line?

 

 
Powered by phpBB® Forum Software