Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Regex to capture field values

by GotToBTru (Prior)
on Jan 13, 2016 at 13:37 UTC ( #1152674=note: print w/replies, xml ) Need Help??

in reply to Regex to capture field values

If the purpose of @emp and @fields is to restrict the output to just those, then you can use them to filter.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @emp = ('riya','vinoth'); my @fields = ('age','company','call','address','country','name'); my %nameh = map { $_, 1 } @emp; my %fieldh = map { $_, 1 } @fields; my @array = ( 'name riya', 'age 34', 'company xxxx', 'fax 8765432109', 'name vinoth', 'age 25', 'call 12345678', 'company xxxx', 'address asdd', 'country Ind', ); my %hash; my $name; while (@array) { my $line = shift @array; if (my ($field, $value) = $line =~ /^(.*) (.*)/) { $name = $value if 'name' eq $field; $hash{$name}{$field} = $value if ($nameh{$name} && $fieldh{$fi +eld}); } else { warn "Invalid line '$line'.\n"; } } print Dumper \%hash;

The output will omit the fax field I added to riya's data because it was not found in @fields.

Update: added name to list of fields! This is what comes of not testing your code.

But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Replies are listed 'Best First'.
Re^2: Regex to capture field values
by vinoth.ree (Monsignor) on Jan 14, 2016 at 13:13 UTC

    Thanks GotToBTru.

    Yes, you are right I wanted to extract for the particular employees, that's why I have @emp array.

    All is well. I learn by answering your questions...

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2021-03-08 20:04 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (127 votes). Check out past polls.