in reply to How to get correct regex

split
use warnings; use strict; while (<DATA>) { chomp; my @cols = split /\s+/, $_, 6; print "@cols[1,3,5]\n"; } __DATA__ 1 F 14 0.06 OPEN 2 F 216 0.97 CONTACT 4 F 326 1.47 LEFT 5 F 232 1.05 MID 6 F 54 0.24 RIGHT 7 F 6 0.03 AT 8 F 6 0.03 AT_GHT 11 F 184 0.83 5DBLT 12 F 151 0.68 2P5D 21 P 21009 94.64 Bin P

Outputs:

1 14 OPEN 2 216 CONTACT 4 326 LEFT 5 232 MID 6 54 RIGHT 7 6 AT 8 6 AT_GHT 11 184 5DBLT 12 151 2P5D 21 21009 Bin P

See also unpack if fixed width applies.

Replies are listed 'Best First'.
Re^2: How to get correct regex
by Anonymous Monk on Jan 13, 2015 at 13:46 UTC

    Thanks for the updated parameters of split, which corrects my version.

      Sorry for the late reply. I like your idea of a regex.
      use warnings; use strict; while (<DATA>) { print "$1 $2 $3\n" if /^\s*(\d+)\s+\S+\s+(\d+)\s+\S+\s+(\w+|\w+\s\ +w+)\s*$/; } __DATA__ ..more text... ..more text... ..more text... Information Summary for Head #1, Site1: No. P Device Percent Name ----------- --- ------------ ------- ----------------- 1 F 14 0.06 OPEN 2 F 216 0.97 CONTACT 4 F 326 1.47 LEFT 5 F 232 1.05 MID 6 F 54 0.24 RIGHT 7 F 6 0.03 AT 8 F 6 0.03 AT_GHT 11 F 184 0.83 5DBLT 12 F 151 0.68 2P5D 21 P 21009 94.64 Bin P ..more text... ..more text... ..more text...
      Bill

        It's working great. Thanks Bill.

Re^2: How to get correct regex
by AtlasFlame (Novice) on Jan 13, 2015 at 13:29 UTC
    I forgot to mention. There are some data above and below the text that i post. Direct split does not work.
      Direct split does not work.

      You need to write some code to step through your data until you get to the data you wish to process, then process data (e.g., with split) until the end of the data that is of interest to you. Then stop.


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