These lines can have any number of items in them. I need to put these into a hash to work with. The approach i am taking is to build each one into a hash reference and then merge it into the main hash.Item1--Item2--Item3 ItemX--Item2--ItemA Item1--ItemV--Item3--Item4
This seems kind of clunky and error prone to me though.#!/usr/bin/perl -w use strict; use Data::Dumper; my $datafile = "data.txt"; open(FILE,$datafile); foreach my $line (<FILE>) { #Kill linefeeds/returns. Data generated on variety of OS's $line =~ s/\n//g; $line =~ s/\r//g; my @items = split(/--/,$line); my $foo = &bhash(\%shash,@items); @shash{keys %$foo} = values %$foo; } close(FILE); print Dumper \%shash; sub bhash { my($hash,@keys )= @_; my $ref= \$hash; map($ref= \$$ref->{$_}, @keys); return $$ref; }
In reply to Generating Hashes from arrays of arbitrary size by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |