in reply to Complex texts parsing
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 |