Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Help Extract these lines

by Laurent_R (Abbot)
on Jun 17, 2013 at 18:52 UTC ( #1039445=note: print w/replies, xml ) Need Help??

in reply to Help Extract these lines

Maybe something like this (quick proposal, untested):

my %my_hash; while <DATA> { chomp; my $id = (split /:/, $_)[1] if /id/; if (/name/) { my $name =~ /: "(\d+)"/; $my_hash{$name} = $id; } }

Replies are listed 'Best First'.
Re^2: Help Extract these lines
by ww (Archbishop) on Jun 17, 2013 at 21:32 UTC


    Yup, Re: Help Extract these lines doesn't even compile.
    Syntax error at Ln 2 ( which, in turn, produces a spurious error re closing curly braces ).

    If you didn't program your executable by toggling in binary, it wasn't really programming!

      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 $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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039445]
LanX predicts problems for Erix next time he lines up at an US immigration counter
[erix]: but tuesdays just as bad
[erix]: thanks for the heads-up but that's a line-up studiiously avoided :P
[LanX]: yeah too far for someone who can't even make it to Amsterdam
LanX looking for a good magit tutorial
LanX ... and git in general
[erix]: I've been to Amsterdam before you were born
[LanX]: 1899?

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2017-08-18 13:19 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (302 votes). Check out past polls.