in reply to parsing text file into a hash?

A hash slice might be handy here:

use strict; use warnings; use Data::Dumper; my %hash; while ( <DATA> ) { chomp; my @vals = split ':'; if ( scalar( @vals ) % 2 ) { @{ $hash{$vals[0]} }{ @vals[1..($#vals/2)] } = @vals[( ($#vals +/2)+1 )..$#vals]; } else { print "Uneven number of elements: $_\n"; } } print Dumper %hash; __DATA__ users:john:mary:1:2 cities:Portland:Amsterdam:New York:Pretty Cool:Awesome:Ugh! misc:foo:bar:baz:Ovid

Read a line and split on the colons. If there are an uneven number of elements (not counting the first), then skip. Otherwise, the first element becomes the hash primary key and the rest of the array is divided in two. The first half becomes the keys to a hash ref identified by $vals[0] and the second half becomes the values.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.