#!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Data::Dumper; use JSON; my @AoH; #Array of hash references foreach my $line (<DATA>) { chomp($line); my ($names, $add, $phone, $email) = split/,/, $line; push @AoH, { name => $names, address => $add, phone => $phone, ema +il => $email } ; } my $json = encode_json \@AoH; 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 =result # Valid JSON [ { "email": " test1@test.com", "name": "Joe", "address": " Main Street", "phone": " 346 20274" }, { "email": " test2@test.com", "name": "Mary", "address": " Central Road", "phone": " 02615128" }, { "email": " test3@test.com", "name": "Lou", "address": " Cannal St", "phone": " 612262297692848" }, { "email": " test4@test.com", "name": "Carl", "address": " Sout St", "phone": " 3268022049187" } ] =cut
In reply to Re: Push values into a hash
by Maresia
in thread Push values into a hash
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |