in reply to Re: help with splitting and printing the array
in thread help with splitting and printing the array

After splitting the line you can simply get rid of the fields starting with '-' and then print the appropriate array elements.
while (<DATA>) { chomp; my @line = grep {!/^-/} split; print $line[0], ' ' , $line[4], "\n"; }

Replies are listed 'Best First'.
Re^3: help with splitting and printing the array
by blindluke (Hermit) on Jan 25, 2015 at 10:09 UTC

    And how will that help when someone switches the order of parameters? In my reply, I've stated that there are two big problems with the split approach. OP already encountered the first one, but your proposed solution won't save him from the second. Relying on the order of commandline parameters is a wrong approach here.

    - Luke

      Hi Luke,

      It doesn't help, it is just a simpler solution. perlnewbie012215 says about 20 lines hence a simple solution might do the job as well.

      BTW your code can be improved by checking the regexp match because it prints out the previous line if the pattern doesn't match:

      if (/-S.+?($servname).+-D.+?($servname).+-C.+?($servname).+$/) { print "Server after -S: $1; Server after -D: $2; Server after -C: +$3;\n"; } else { print "Unknown record structure: $_\n"; }
      Or the important command line switches could be checked one by one and add them to the output if they exist.

        If it doesn't help, then it's only simple, not a solution. You should not suggest a split / field index based method when it is clear that the field indexes and their order might easily change, due to the (known) nature of the input data. Especially since this potential problem has already been pointed out to the OP.

        As to error checking - it's not an 'improvement', it's a necessity. It's obvious that before the OP uses this in production code, he should check for possible error, both upon matching and upon opening his input file.

        - Luke