I'm trying to read a datafile and append data onto a specific line as indexed/identified by the first word.
The data file is in this formatname,success1,description1,success2,description2,...
eg:My attempt at doing this is below. Apart from it not working, it seems to hang on the 'split' linedog,yes,string1,yes,string2 cat,no,string3 mouse,no,string4,no,string5 horse ferret
Can any one help and show me the correct way, or even a better way, of doing this??#!/bin/perl use strict; use warnings; use Data::Dumper; my $name="mouse"; open (FH, "datafile.txt")||die "cannot open file"; while ( <FH> ) { next unless s/^(.*?):\s*//; $HoA{$1} = [ split /,/ ]; } print Dumper(%HoA); my $success="yes"; my $description="new string"; push @{ $HoA{$name} }, $success, $description; for $venue ( keys %HoA ) { print FH "$venue @{ $HoA{$venue} }\n"; }
In reply to Trying to read a file into a Hash of Arrays by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |