Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The json_decode in PHP there looks like the same thing as decode_json in the JSON (or JSON::XS) module in perl. Note, however, that PHP is kinda lying to you - key-value pairs are stored in maps or, in perl, hashes. That PHP doesn't differentiate may make PHP easier, but makes translating what you've learned in PHP to other languages harder. (To be fair, translating from Perl to, say, C++, isn't trivial, either, for similar abstraction-based reasons.)

Presumably, that get_object_vars function exists elsewhere in the code, though your printouts make it look like it doesn't do anything, as $value1 already will have that value by way of the decode_json function. So, a rough copy would be:

#!/usr/bin/perl use JSON; my %data; while (<DATA>) { my ($key, $value) = split ' ', $_; $value = decode_json($value); if (exists $data{$key}) { while (my ($k,$v) = each %{$value[1]}) { $data{$key}{$k} += $v; } } else { $data{$key} = $value; } } use Data::Dumper qw(Dumper); print Dumper(\%data); __END__ 36dc0d7d0ac25ce60898c36ca135fbbd [[12051,840,501,33],{"23602":22}] 4c38528ffe96a15c90e8cfcaaad048e3 [[13308,124,-1,62],{"8002":12}] 5557a6bed3793133754d288e2b58763a [[2197,840,751,6],{"16501":1}] 5a9c1f69434c1a8b1d7880ef03ae4264 [[7525,616,-1,14347],{"24902":37}] 87f63173118df680a4c1d63b7953faf3 [[2765,458,-1,11937],{"3102":15}] 901d1a5dbd4ed87fd68db2513fb29762 [[1828,124,-1,63],{"8002":379}] c23a2b2c10af8af96b1b24ddd4cc53d4 [[62,840,820,38],{"16801":303}] d7af9cd8573ecbec6d42e453439e3e0f [[4680,124,-1,63],{"1012":1896}] d93adab6b345608d38ea84811012dce8 [[114,840,819,48],{"22502":322,"80 +02":3}] ffd50dd8b4986f40634d6b5925dc04c6 [[6089,840,803,5],{"1252":1}]
Not sure if that's entirely correct, but it seems to work.


In reply to Re: Converting some MapReduce PHP scripts to Perl by Tanktalus
in thread Converting some MapReduce PHP scripts to Perl by regan99

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found