Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: help with splitting and printing the array

by pme (Monsignor)
on Jan 24, 2015 at 22:16 UTC ( [id://1114394]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1114394]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found