Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Help Extract these lines

by Laurent_R (Parson)
on Jun 17, 2013 at 22:25 UTC ( #1039480=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Help Extract these lines
in thread Help Extract these lines

Alright, I just typed an approach for a solution directly on the web page, not having access to a Perl compiler when I did it (and I said that I could not test). What I typed does not compile, but it takes less than 5 seconds to find out why and to correct it. One really easy to find compile error (adding parens after the while keyword) and one other small stupid mistake just as easy to find and to correct, and it basically works:

use Data::Dumper; my %my_hash; my $id; while (<DATA>) { chomp; s/\r//g; $id = (split /:/, $_)[1] if /id/; if (/name/) { my $name = $1 if /\: "(\w+)"/; $my_hash{$name} = $id; } } print Dumper \%my_hash; __DATA__ "id": "xx-ada-qwebasd", "name": "telphone", "id": "fasda-asd-123123-fkja123a", "name": "car", "id": "97f921-a312-fas2", "name": "ball",
which prints:
$ perl 2_lines.pl $VAR1 = { 'ball' => ' "97f921-a312-fas2",', 'car' => ' "fasda-asd-123123-fkja123a",', 'telphone' => ' "xx-ada-qwebasd",' };
I admit the data still needs a bit more cleaning, but that's really boilerplate code, and I think that this untested code basically gave a very good track towards the solution.


Comment on Re^3: Help Extract these lines
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-10-01 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (41 votes), past polls