[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] a regex question



On 9/27/99 at 11:59 PM, Nicholas G. Thornton wrote:

: I'm having problems with a little regex thing here. What I'm trying to do is 
: read from a data file and out put in a certain way. If the data file looks 
: something like...
: 
: #####
: #  TEXT/ttxt --> SimpleText
: #  TEXT/MOSS --> Netscape
: #                                          #
: #  ttro/ttxt --> SimpleText read only
: #                                          #
: #  W6BN/MSWD --> MS Word 6.0/95
: #                                          #
: #  W8BN/MSWD --> MS Word 97-8
: #                                          #
: #  GIFf/ogle --> PictureViewer GIF
: #####
: 
: I want the output to be something like...
: 
: #####
: #  TEXT     ttro     W6BN     W8BN  #
: #  GIFf                             #
: #####
: 
: The code snippit I currently have is...
: 
: open(DATA, "<cnvtOS data");
: print "\n\t\t#####\n";
: while ($line = <DATA>) {
:      $sub_string = substr($line,3,4);
:      if ($line =~ /^#  ....\//) {
:           $a_mime_type = $sub_string;
:      }
:      if ($sub_string ne $a_mime_type) {
:           print "$a_mime_type\n";
:      }
: }
: print "\n\t\t#####\n";
: __END__
: 
: Which prints each unique $sub_string on a new line. By replacing the "\n" with '     
: ' I can get them on one line. But all my attempts to line-wrap after, say, four 
: of them have failed. Cound someone lead me in the right direction?
: 
: 

You need a counter that signals when to print a return, here's a better version
(with comments):

# preset some variables
my $count = 0;
my $a_mime_type = "";

open DATA, "<cnvtOS data";
print "\n\t\t#####\n";

# no need to assign the input line to a variable, $_ is implicit
# in the following while and in the pattern match
while (<DATA>) {
    
    # skip the input line if it does not match the pattern
    # for mime types, otherwise, $1 will contain the mime type
    next unless /^#  (....)\//;
    $sub_string = $1;
    
    # skip the input line if the extracted mime type matches
    # the previous one, since we will have already printed it
    next if ($a_mime_type eq $sub_string);
    
    # otherwise, this is a new mime type, save it, then print it
    $a_mime_type = $sub_string;
    print "$a_mime_type   ";
    
    # increment the counter
    $count++;
    
    # if the counter is less than 4, skip to the next input line,
    # otherwise, print a return and reset the counter
    next if ($count < 4);
    print "\n";
    $count = 0;
}
print "\n\t\t#####\n";
__END__



Don



# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org