in reply to Problem with split and file processing. Need Help!!!
Instead of chained ifs, you can use hash keys to index into an array.
my %keys = qw( monster 0 hp 1 size 2 AC 3); my @data = ( [qw(troll 30 10 2)], [qw(dwarf 20 2 9 )], [qw(pixie 2 1 30 )]); foreach my $line (@data) { print "name @$line[$keys{'monster'}] \n"; print "\thas @$line[$keys{'hp'}] hit points\n"; }
prints something like this:
name troll has 30 hit points name dwarf has 20 hit points name pixie has 2 hit points
Of course, by then, you're just one step away from creating objects. Which is probably a better idea in the long run.
|
|---|