
Laurent_R
Enthusiast
Nov 27, 2012, 12:26 AM
Views: 857
|
|
Re: [preston3271] How to isolate data elements with a data block with (\n) newlines????
|
|
|
Hi, there is nothing really mysterious in my code suggestion. Here is the code thougoughly commented.
my ($request, $node); # declare two variables while (<DATA>) { next unless /^RequestID/ or /^Nodename/; # discard useless lines chomp; # removes trailing \n my($name, $id) = split /:\s+/, $_; # splits the line on colon (:) followed by any number of spaces # note that ($name, $id) is not really an anonymous array, but simply a list of two fields # we have two types of line, so $name may contain RequestID or Nodename, and $id contains the corresponding value # the next two lines just check what there is in the first field to decide what to do with the $id value $request = $id if $name eq "RequestID"; # if the line was a RequestID line... $node = $id if $name eq "Nodename"; # if it was a Nonename line if (defined $request and defined $node) { # the only slightly tricky part: we print the result only if we have met both types of lines print "RequestID $request on nodename $node \n"; ($request, $node) = (undef, undef); # reset the two variables to undef to make "defined" test possible } } Please don't hesitate to ask if you still don't understand something.
(This post was edited by Laurent_R on Nov 27, 2012, 4:06 AM)
|