in reply to Push values into a hash

What is that line doing? How does your code behave differently when you leave it out?

push \%data, $data{name}, $data{address}, $data{phone}, $data{emai +l};

Where is $number used? What is $claim?

You could make your code a bit more data-driven by doing the assignment through a list of pairs in a loop:

my %field_name_mapping = ( n_names => 'name', n_add => 'address', n_phone => 'phone', n_email => 'email', ); ... for my $source (sort keys %field_name_mapping) { my $target = $field_name_mapping{ $source }; $data{ $target } = $claim->{$source}; };

Replies are listed 'Best First'.
Re^2: Push values into a hash
by Anonymous Monk on Jul 25, 2016 at 15:08 UTC
    This should be this:
    $data{name} = $number->{"n_names"}; $data{address} = $number->{"n_add"}; $data{phone} = $number->{"n_phone"}; $data{email} = $number->{"n_email"} || '';

      You keep trying to push \%data. What is that supposed to accomplish?

      I don't understand what you're trying to do here. A hash always consists of a key and a value. You at best have a value here, never mind the syntax.

      Maybe you want to use a real array, @data instead of a hash?

      I think in addition to your code, you should also show what data structure you want at the end.

        Hi, I posted a better sample code to show it better. I would like to end up with json data as:
        ["name:Joe","address:Main Street","phone: 346 20274","email:test1@test +.com"] ...

        thanks!
Re^2: Push values into a hash
by Anonymous Monk on Jul 25, 2016 at 15:32 UTC
    Hi, is a better sample of what I am trying to do:
    #!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Data::Dumper; use JSON; my %data; foreach my $line (<DATA>) { chomp($line); my ($names, $add, $phone, $email) = split/,/, $line; push \%data, { name => $data{name}, address => $data{address}, pho +ne => $data{phone}, email => $data{email} }; } my $json = encode_json \%data; print Dumper $json; __DATA__ Joe, Main Street, 346 20274, test1@test.com Mary, Central Road, 02615128, test2@test.com Lou, Cannal St, 612262297692848, test3@test.com Carl, Sout St, 3268022049187, test4@test.com
      I suspect you want an array, not a hash (push is an array operation) i.e.
      my @rows; ... # rest of code push @rows, { ... }; ... my $json = encode_json \@rows;
      update: Renamed variable
      And what do you want the output for those four records to look like exactly?
      mistyped:
      push \%data, { name => $names, address => $add, phone => $phone, email + => $email };