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

Re: [MacPerl] a regex question



Nicholas,

Why don't you collect the input in a hash in one pass and output it in another?
(Two output methods shown below.)

#!perl -w
open DATA,  "<cnvtOS data"  or die "unable to open cnvtOS for input";

#
#  Read in and accumulate unique file types.
#
my %file_types = ();
my $line;
while (defined($line = <DATA>)) {
        if ( $line =~ m[#\s*(....)/....\s*-->] ) {
                $file_types{$1} = $1;
        }
}
close DATA;

#
#  Output the bits under detailed programmer's control.
#
my $item_counter = 0;
my $item;
print "\n\t\t#####\n";
foreach $item ( sort keys %file_types ) {
        print "\t\t#" if $item_counter % 4 == 0;
        print $item, "\t";
        print "#\n"  if  ( $item_counter % 4 ) == 3;
        $item_counter++;
}
if ( $item_counter % 4 > 0 ) {
        print "    \t" x ( 4 - $item_counter % 4 ), "#\n";
}
print "\t\t#####\n";

#
#  Or use the devious power of a Practical Extraction and Report Language
#  (aka Perl) ...
#
my @x = sort keys %file_types;
push( @x, '' ) while $#x % 4 != 3;  # filler elements to mod 4 length
while ( $#x >= 0 ) {
    ($x0,$x1,$x2,$x3) = splice @x,0,4;
    write;
}
print "\t\t#####\n";

format top =
		#####
.
format STDOUT =
		#    @<<<    @<<<    @<<<    @<<<    #
$x0,$x1,$x2,$x3
.
__END__


Nicholas said:
>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.
> ... input ...
#  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                             #
>#####
>


--
Regards, Larry F. Allen-Tonar        (larryat@cts.com) +1 760/746-6464 (voice)
         Principal Designer                              +1 760/746-0766 (FAX,
         P.O. Box 463072                                         upon request)
         Escondido, CA  92046-3072
"Futuaris nisi irrisus ridebis.", Carlton in _The Road to Mars_ by Eric Idle



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