in reply to Hash Add new elements through loop
Why jump through all those hoops? On the face of it all you need is:
#! /usr/bin/perl env use strict; use warnings; use Data::Dumper; my %record; while (my $line = <DATA>) { chomp $line; my ($key, $val) = split /:\s*/, $line, 2; $record{$key} = $val if defined $val; } print Dumper (\%record); __DATA__ Name: Sam Address: Mascot Ph.No: 123321
Prints:
$VAR1 = { 'Ph.No' => '123321', 'Address' => 'Mascot', 'Name' => 'Sam' };
Note though that I added a chomp (most of your values would otherwise have a trailing new line), limited split to finding two elements to avoid chopping off the end of any value that happens to contain a colon and check to see that there is in fact a value field on the line.
|
|---|
| Replies are listed 'Best First'. |
|---|