I just want to point out a small error in your code, which in this case proved to be no error at all - but just because you were lucky. You wrote:
while (<DB>) { @_ = split/[\t|,|\||\n]/; # here things go accidently righ +t ;-) $data{ $_[0] } = { @_[1..$#_] }; }
You wanted to split on <tab>, <,>, <|> or <newline> and you used a character class for that. A character class matches any of the characters in that class, e.g. [aeiou] matches any one vocal and you must not use an alternation character <|> in the class! The code worked nevertheless as - by chance - the <|> is part of the character class. It need not be escaped, but you can do it if you want. If you want to use alternation than you would have to write
but this is not efficient. The character class is the better idea. So you would end up with@_ = split /\t|,|\|\n/;
@_ = split /[\t\n,|]/;
Furthermore, if you are not sure whether there is additional space around the delimiters this would change to
@_ = split /[\s,|]+/;
allowing for one or more of the chars in the character class. I also removed the split on the ending <newline> as this only produces an empty field at the end which is not returned by split.
-- Hofmator
In reply to Re: Re: Stuck while learning about the hash
by Hofmator
in thread Stuck while learning about the hash
by Stamp_Guy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |