You can use this snippet to only get those items that have one token before the first semi-colon, and also one token after that semi-colon:my @line = qw(foo;bar baz;qux user;errors;suck trust; ;no-one ;);
I chose not to worry about possible whitespace, it is ugly enough as it is. The idea is to do as guha rightfully said, use a regex. The map filters out the key and value pairs, but because of tainted user input, we still get an undefined key (actually, three candidates that overwrite each other). The grep takes care of those.my %hash = grep !/^$/, map { ($a,$b) = $_ =~ /^([^;]+);([^;]+)/; ($a,$b); } @line;
Warning: I use $a and $b in this example. These variables are meant to be used with sort, but i see no problem in using them here - you don't even have to declare them. Just don't be tempted to use them for any task more than temporary. In fact, you should probably just stick to only using them for sort. ;)
Also, a time saving tip: If you want to quickly see if the hash (or array or any other data strucure) contains what you think it should, use Data::Dumper (thanks to guha for clearing up my blunder - see below ;))
From the first two code snippets, the variable %hash should look like:use Data::Dumper; print Dumper \@array; print Dumper \%hash; print Dumper $reference;
$VAR1 = {
'foo' => 'bar',
'baz' => 'qux',
'user' => 'errors'
};
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
In reply to (jeffa) Re: How to assign a hash from an array?
by jeffa
in thread How to assign a hash from an array?
by jamen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |