Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi,

I have got an array with person details, from that array I need to get set of fields and values, some of the person may not have all the fields.

As there are no delimiter between each person, I tried below code like, first converted the array as single string and did regular expression matching.

As I am searching in a string, if a field is missing for a person its matching with next person value.

Example: if u run the above code you will get the below output

$VAR1 = { 'country' => '', 'name' => 'riya', 'address' => '', 'call' => '12345678', 'company' => '', 'age' => '34' }; $VAR1 = { 'country' => '', 'name' => 'vinoth', 'address' => '', 'call' => '12345678', 'company' => '', 'age' => '25' };

Here the riya user has no call field, but it taking the vinoth's call value, as per the regex its the expected behavior. how to avoid this behavior? I hope little change in the regex could resolve my issue.

use strict; use warnings; use Data::Dumper; my @fields=('age','company','call','address','country'); my @emp=('riya','vinoth'); my @array=( 'name riya', 'age 34', 'company xxxx', 'name vinoth', 'age 25', 'call 12345678', 'company xxxx', 'address asdd', 'country Ind'); my $details = join(',',@array); print "$details\n"; foreach my $name (@emp) { my %hash=(); $hash{'name'}=$name; foreach my $field (@fields){ if ($field eq 'name'){ if ($details =~/name\s*$name.*?,$field\s*(\S+),/g) { $hash{$field}=$1; } else{ $hash{$field}=''; } } elsif($field eq 'age'){ if ($details =~/name\s*$name.*?,$field\s*(\d+),/g) { $hash{$field}=$1; } else{ $hash{$field}=''; } } elsif($field eq 'company'){ if ($details =~/name\s*$name.*?,$field\s*(\S+),/g) { $hash{$field}=$1; } else{ $hash{$field}=''; } } elsif($field eq 'call'){ if ($details =~/name\s*$name.*?,$field\s*(\d+),/g) { $hash{$field}=$1; } else{ $hash{$field}=''; } } elsif($field eq 'country'){ print "$field\n"; if ($details =~/name\s*$name.*?,$field\s*(\S+),/g) { $hash{$field}=$1; } else{ $hash{$field}=''; } } elsif($field eq 'address'){ print "$field\n"; if ($details =~/name\s*$name.*?,$field\s*(\S+),/g) { $hash{$field}=$1; } else{ $hash{$field}=''; } } } print Dumper \%hash; }

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

In reply to Regex to capture field values by vinoth.ree

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2020-09-20 06:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If at first I donít succeed, I Ö










      Results (119 votes). Check out past polls.

      Notices?