M
Michael Hamer
Hi,
the Perl Cookbook suggests in Recipe 11.10 "Reading and Writing Hash
Records to Text Files" the following code to read a hash from a file:
$/ = ""; # paragraph read mode
while (<>) {
my @fields = split /^([^:]+):\s*/m;
shift @fields; # for leading null field
push(@Array_of_Records, { map /(.*)/, @fields });
}
or, a bit simpler for testing:
$text="a:b\nc:d\ne:f";
my @fields = split /^([^:]+):\s*/m,$text;
foreach $key (@fields)
{
print "field is $key\n";
}
This code works as intended, but I don't understand it. Why is
$fields[0] empty after the split? I would have expected it to contain
the "a" but that is found in $fields[1].
the Perl Cookbook suggests in Recipe 11.10 "Reading and Writing Hash
Records to Text Files" the following code to read a hash from a file:
$/ = ""; # paragraph read mode
while (<>) {
my @fields = split /^([^:]+):\s*/m;
shift @fields; # for leading null field
push(@Array_of_Records, { map /(.*)/, @fields });
}
or, a bit simpler for testing:
$text="a:b\nc:d\ne:f";
my @fields = split /^([^:]+):\s*/m,$text;
foreach $key (@fields)
{
print "field is $key\n";
}
This code works as intended, but I don't understand it. Why is
$fields[0] empty after the split? I would have expected it to contain
the "a" but that is found in $fields[1].