in reply to Complex texts parsing

My code below parses the file as you describe.
However, there is some logic you seem to be missing. In particular, you show a "1" in the next-to-last field for the Remote printer entry, but your description doesn't allow for that.
Hopefully you'll be able to take this code and enhance it.
my $data = do { local $/; <> }; # or however you load it. my( %device, %system ); for ( split /\n(?=\S)/, $data ) { if ( /^device for (.*?): (\S+)/ ) { $device{$1} = $2; } elsif ( /^system for (.*?): (\S+)/ ) { $system{$1} = $2; } elsif ( /^printer (\S+)/ ) { my $p = $1; my %printer = ( name => $p, locus => $device{$p} ? "Network" : $system{$p} ? "local" : "Remo +te", description => (/Description: (.*)/)[0], up => "Up", zero => 0, ); if ( $system{$p} ) { $printer{'interface'} = $system{$p}; $printer{'remote'} = "N/A"; } else { ( $printer{'interface'} ) = /Interface: (.*)/; $printer{'interface'} ||= ''; # in case it's absent $printer{'interface'} =~ s,.*/,,; # strip path info $printer{'remote'} = "no"; } print join('|', @printer{qw( name locus interface description up z +ero remote )}), "\n"; } }

jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.

Replies are listed 'Best First'.
Re: Re: Complex texts parsing
by kirk123 (Beadle) on Feb 07, 2003 at 22:42 UTC
    Thanks jdporter, Hopefully one day I can help someone else in need.