Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to Map Regex to Hash

by vegasjoe (Sexton)
on Jun 30, 2006 at 20:22 UTC ( [id://558677]=perlquestion: print w/replies, xml ) Need Help??

vegasjoe has asked for the wisdom of the Perl Monks concerning the following question:

I have a string that I would like to put into a hash.
0,"104"1,"Assign Tracking Numb"29,"905745371116"526,"0201"99,""
The string is setup like the following: Code,"data". The code is always numeric. The desired output is similiar to following:
%rec = ( 0 => 104, 1 => 'Assign Tracking Numb', 29 => 905745371116, 526 => 0201, 99 => , );
The regex needs to be change to something else.   /((\d*),"\w*")/gm;
$result = "0,\"104\"1,\"Assign Tracking Numb\"29,\"905745371116\"526,\ +"0201\"99,\"\""; %row = $result =~ /((\d*),"\w*")/gm; use Data::Dumper; print Dumper(%row); foreach $key (keys (%row) ) { print $key ."\n"; } Output (Not what I want) $VAR1 = '0,"104"'; $VAR2 = '0'; $VAR3 = '29,"905745371116"'; $VAR4 = '29'; $VAR5 = '526,"0201"'; $VAR6 = '526'; $VAR7 = '99,""'; $VAR8 = '99'; 0,"104" 29,"905745371116" 526,"0201" 99,""

Replies are listed 'Best First'.
Re: How to Map Regex to Hash
by Roy Johnson (Monsignor) on Jun 30, 2006 at 21:04 UTC
    I think split is a better fit.
    %row = split /,"?|"/, $result;
    seems to work. That's a comma followed by an optional doublequote, or just a doublequote.

    Update: CountZero is right: there aren't any commas without a doublequote after them. So I guess it could be

    %row = split /,?"/, $result;

    Caution: Contents may have been coded under pressure.
      %row = split /,"|"/, $result; seems to work as well.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How to Map Regex to Hash
by japhy (Canon) on Jun 30, 2006 at 20:36 UTC
    I think the regex /(\d+),"(.*?)"/g will work for you. This assumes no embedded quotes, etc. The problem with yours was the parens on the outside of the regex. And that /m modifier was useless.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: How to Map Regex to Hash
by jwkrahn (Abbot) on Jun 30, 2006 at 20:36 UTC
    It looks like you probably want something like:
    my %row = $result =~ /(\d+),"([^"]+)"/g;
    Update: moved the capturing parentheses.
Re: How to Map Regex to Hash
by TedPride (Priest) on Jul 01, 2006 at 01:05 UTC
    Split doesn't work here - the edge cases don't process properly. japhy had the correct answer:
    use strict; use warnings; use Data::Dumper; chomp($_ = <DATA>); my %rec; $rec{$1} = $2 while m/(\d+),"(.*?)"/g; print Dumper(\%rec); __DATA__ 0,"104"1,"Assign Tracking Numb"29,"905745371116"526,"0201"99,""
      What edge case where you thinking of?

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found