If dealing with makes by themselves is unnecessary, you can even use simulated multilevel hashes (saving some memory if there were a large number of makes and models) by changing this line:#!/usr/bin/perl use strict; use warnings; my %hash; my $make; while( <DATA> ) { chomp; if( /^(\S.*)/ ) { $make = $1 } elsif( /^\s+(\S.*)/ ) { $hash{$make}{$1} = 1 } elsif( /^\s*$/ ) { next; } } use Data::Dumper; print Dumper( \%hash ); # Valid - prints "Valid..." my ($make1, $model1) = qw(Nissan Maxima); print "Valid $make1 model1\n" if $hash{$make1}{$model1}; # Not valid - doesn't print my ($make2, $model2) = qw(Toyota Mustang); print "Valid $make2 model2\n" if $hash{$make2}{$model2}; # Valid - prints "Valid..." my $make3 = "Ford"; print "Valid $make3\n" if $hash{$make3}; __DATA__ Honda Civic Accord Toyota Camry Corolla Tundra Nissan Maxima
to this:elsif( /^\s+(\S.*)/ ) { $hash{$make}{$1} = 1 }
And then validate with this:elsif( /^\s+(\S.*)/ ) { $hash{$make,$1} = 1 }
print "Valid $make1 $model1\n" if $hash{$make1,$model1};
In reply to Re: hashes with arrays
by runrig
in thread hashes with arrays
by magawake
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |