[oclug]Really basic Perl help

Milan Budimirovic milan.budimirovic at sympatico.ca
Sat Jan 4 17:48:22 EST 2003


On Sat, 2003-01-04 at 16:40, B McKee wrote:
> I'm sure I am the most confused one here :-)
> 
> The result I really want is  "Varous text I want"
> 
> The result I was hoping for was "Three Various text I want \n Three"
> 
> ----  Assuming this is contents of data.txt
> Line One with stuff
> Line Two with other stuff
> Line Three Various text I want
> Three
> 
> One of the reasons I'm confused is I want to look at the file as a whole 
> rather than a series of lines.  My other choice seems to be @a, but that just 
> gives me an array, which isn't what I want either.
> 
> In truth I want to be able to read in an HTML file and output a subsection 
> that is framed with a specific <--! Comment -->  
> I gather their may be a module that would help, but I thought my needs could 
> be handled by a simple regular expression search.  Since this will end up on 
> someone else's server I didn't want to have to install any extra software to 
> make it work.
> 

If I understand correctly, you want to use Perl's regular expressions to
match the line you want, and leave the rest. This shouldn't be too
tricky, except for the possibility that the comment spans two or more
lines.

Basically, you will want to concatenate the contents of the file into
one string, then try to match your pattern.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
use IO::File;

#Open the file for reading
$input = IO::File->new("< $path/$filename")
                       or
           die "Could not open $path/$filename for reading: $!\n"; #"
  
# Stuff everything into one string
while (defined ($line = $input->getline())) {
  $string .= $line;
}

# Grab the comment using a regular expression
while ($string =~ m/<--! (.*) -->/) {
  $comment = $+;
  last;
}

print "$comment\n";
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


This is a brute-force method but it should work. If it's a very large
file you may want to search out the "<--" before adding it to the
string.





More information about the OCLUG mailing list