On Nov 2, eventualdeath said:

>Statement 1 :-
>@read = join ('<hr>\n', split(/<hr>/, @read));

Well, you don't want to use join() here.  join() takes a LIST and returns
a STRING.  If you use it, your array will have ONE element.  That doesn't
sound terribly useful.  (And you've got '<hr>\n' where you probably wanted
"<hr>\n".)

That being said, I have a different solution for you -- one that gets
around the issue entirely.

>open (FILE, "frall.html")||  die"file not found $!\n";
>@read = <FILE>;
>chomp @read;
>@read = map "$_<hr>\n", split /<hr>/i, join '', @read;

In other words, each element in @read should be a block of text up to the
next occurrence of <hr>, right?  So wouldn't it be nice if you could tell
Perl to read a "line" of the file as something ending in <hr> instead of
something ending in \n?  What I'm trying to say is, you can tell Perl to
read a "line" as you want it to.

This is closely related to an article I just wrote, "Getting a Handle
on Files"[1].  You want to set $/ (the input record separator) to the
value "<hr>" (or perhaps "<HR>") when reading from the HTML file.  Why?
Because that tells Perl that a "line" ends with "<hr>".  The default value
for $/ is "\n", which is why Perl reads what we commonly think of as a
line when you use <FILE>.  However, a "line" is really just a very
specific record.  A record is a chunk of data.  A line happens to be a
record that ends at the first newline encountered.  You don't care about
newlines for your program, though, you care about <hr> tags.

Here's a fix to your program:

  open FILE, "< frall.html" or die "Can't read frall.html: $!";
  {
    # we local()ize $/ here so that it's only changed in this block
    local $/ = "<hr>";  # maybe you want "<HR>"
    @read = <FILE>;
  }
  close FILE;

Now you can work with @read, and it will have the data you'd like it to.   


[1] http://www.pobox.com/~japhy/articles/misc/files.html

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to