in reply to Re: Push values into a hash
in thread Push values into a hash

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

Replies are listed 'Best First'.
Re^3: Push values into a hash
by Arunbear (Prior) on Jul 25, 2016 at 16:34 UTC
    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
Re^3: Push values into a hash
by Anonymous Monk on Jul 25, 2016 at 16:21 UTC
    And what do you want the output for those four records to look like exactly?
Re^3: Push values into a hash
by Anonymous Monk on Jul 25, 2016 at 15:34 UTC
    mistyped:
    push \%data, { name => $names, address => $add, phone => $phone, email + => $email };