in reply to Parsing output with a special format
Hoping i'm understandunding correctly the following statement:
> because there is no smaller number than 1234 and larger than 1220
You can use a switch (do something only if switch is on) and a yardstick/touchstone (do only if comparison gives something..). In loops generally exit condintions go first, but in this case the switch condition must be the first one, then all exit conditions as always.
use strict; use warnings; my $arg = 1220; my $switch; my @result; # here the touchstone or yardstick # start the comparison with a reasonably big number $result[0] = 10 ** 6; while (<DATA>){ $switch = 1 if /^Status\s+id.*name$/; next unless $switch; next if /^[\-]+$/; chomp; my @fields = split /\s+/; if ( $fields[1] =~ /^\d+$/ and $fields[1] > $arg and $fields[1] < + $result[0] ){ @result = @fields[1,0,-1]; } } # reset if nothing found undef @result unless $result[1]; print join ',',@result; __DATA__ date:17/4/2000 version: 4.1.0 -more info- -more info- ------------------------------------------------------------------ Status id not important info (two columns) name ----------------------------------------------------------------- Run 1234 @ ... foo1 Err 1235 @ ... foo1 Wait 54123 @ ... foo2 -----------------------------------------------------------------
L*
PS i add here to answer your below questions:
about do not start with a reasonable big number: know your data! is a very good principle. I cannot help here. You can use the biggest number perl can handle on you platform..
About the @fields[1,0,-1] it is an array slices. Note the Perl is happy within negative indexes so, as said in the CB but also here for posterity, means the second, the first and the last elements.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing output with a special format -- switch and yardstick
by ovedpo15 (Pilgrim) on Jun 05, 2018 at 10:26 UTC | |
by AnomalousMonk (Archbishop) on Jun 05, 2018 at 13:13 UTC | |
by ovedpo15 (Pilgrim) on Jun 05, 2018 at 12:55 UTC |