CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [preston3271] How to isolate data elements with a data block with (\n) newlines????: Edit Log



Laurent_R
Veteran / Moderator

Nov 27, 2012, 12:26 AM


Views: 1575
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.



Code
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)


Edit Log:
Post edited by Laurent_R (Veteran) on Nov 27, 2012, 4:06 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives