in reply to Parsing output with a special format

Hello ovedpo15

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.

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

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
    It is a very nice solution, thank you.
    Is it possible, somehow to not to start with a reasonably big number?  $result[0] = 10 ** 6;
    I don't know the 'max' id it get.
    By the way, in  @result = @fields[1,0,-1]; how does it work? what is the '-1' in the end?
      ... @result = @fields[1,0,-1]; how does it work?

      Please see Slices in perldata.

      what is the '-1' in the end?

      It's an array index. (Update: See Subscripts in perldata.) In Perl, negative indices access array elements from the end of the array:

      c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = qw(first second some other stuff penultimate ultimate); ;; printf qq{$_ } for @ra[ 1, 0, -2, -1 ]; " second first penultimate ultimate


      Give a man a fish:  <%-{-{-{-<

      will it be easier to find only the smallest id number after the current id number argument? so the function will return only id and not array.
      In the example above it will return only 1234. How should I implement it?