I'm definately not an expert, but I've got a similar script that I use to parse a file in the same way. This is the code I wrote to do so, modified to work with your file. I'm sure there is a better way to do this. It puts whatever is between the *DESCRIPTION and the *END into the @description array. You can then loop over @description and do what you wish with it.
#!/bin/perl -w
use strict;
open (DATA, "text.txt") || die "Can't access test\n";
#each element in the array is a single line
my @data = <DATA>;
close(DATA);
#vars to manipulate the data
my $line="";
my @description = ("");
my $found = "false";
#while it is not the end of the file
while($line = shift @data){
chomp $line;
if($line =~ "DESCRIPTION"){
$line = shift @data;
chomp $line;
while(!($line =~ "END")){
if(!($line =~ ";")){
push @description, $line;
}
$line = shift(@data);
chomp $line;
}
print "[",@description, "]\n";
#if there's going to be more descriptions in the same file
#then we can clear the array and keep going through the
#file until we run out of lines. Otherwise @description
#will continue to accumulate strings
#@description = ("");
}
}
if($found eq "false"){
#then we didn't find a description
#add it here
}
That will at least answer your first question.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|