Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Storing String from Line Before Regex Match

by toolic (Bishop)
on Mar 31, 2016 at 18:32 UTC ( [id://1159217]=note: print w/replies, xml ) Need Help??


in reply to Storing String from Line Before Regex Match

A different approach is to read the file as records separated by a blank line. Store the data into a hash for each record, then print out only what you need. One benefit is that this method is independent of the order of the lines of the input.
use warnings; use strict; $/ = "\n\n"; while (<DATA>) { my %data; for my $line (split /\n/) { my ($k, $v) = split /\s*:\s*/, $line; $data{$k} = $v; } print "$data{'First Name'} $data{'Last Name'}\n" if $data{Location +} eq 'Central USA'; } __DATA__ First Name: John Last Name: Doe Occupation: Network Administrator Location: West Coast First Name: Jane Last Name: Doe Occupation: Human Resources Location: East Coast First Name: James Last Name: Doe Occupation: Technical Support Engineer Location: Central USA

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found