Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trying to read a file into a Hash of Arrays
by Athanasius (Archbishop) on Feb 20, 2015 at 04:46 UTC | |
|
Re: Trying to read a file into a Hash of Arrays
by vinoth.ree (Monsignor) on Feb 20, 2015 at 04:46 UTC | |
|
Re: Trying to read a file into a Hash of Arrays
by LanX (Saint) on Feb 20, 2015 at 04:46 UTC | |
|
Re: Trying to read a file into a Hash of Arrays
by sandy105 (Scribe) on Feb 20, 2015 at 10:42 UTC | |
by LanX (Saint) on Feb 20, 2015 at 15:42 UTC |