in reply to Re: Stuck while learning about the hash
in thread Stuck while learning about the hash

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

@_ = split /\t|,|\|\n/;
but this is not efficient. The character class is the better idea. So you would end up with
@_ = 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.

See also split and perlre.

-- Hofmator

Replies are listed 'Best First'.
Re: Re: Re: Stuck while learning about the hash
by hackmare (Pilgrim) on Jun 08, 2001 at 18:41 UTC

    Here's my 10-ct's worth. This is a bit after all the other posts, so it's probably not much help, (insert random excuse here about having to work next to my client) but I decided to post it anyways.

    I got lazy and I also noticed that this is a question about learning to use hashes so I'm keeping the hash references down to a minimum and am using the key to point to useful data. However, doing this loses the sorting capabiltiy that a hash of hashes would allow, so it's sub-optimal.

    #!/usr/bin/perl -w use strict; my %items; open(DB, "db.txt") || die "Could not open the database: $!"; while(<DB>) { chomp; my @record = split(/\t/); my $scottnum = shift @record; my @details = split(/\|/, shift @record); foreach my $detail (@details) { my @item = split(/,/, $detail); if ( ($item[0]) && ($item[1]) ){ $items{$scottnum.'-'.(shift @item)} = shift @item; } } } foreach my $key (sort (keys %items) ) { my ($sn,$mk) = split ('-',$key); print "key: $key \tmk: $mk\t sn: $sn item: $items{$key}\n"; }

    The script returns:

    key: 614-16.00 mk: 16.00 sn: 614 item: MNH key: 614-32.00 mk: 32.00 sn: 614 item: USED key: 614-50.00 mk: 50.00 sn: 614 item: PB key: 615-12.00 mk: 12.00 sn: 615 item: USED key: 615-36.00 mk: 36.00 sn: 615 item: MNH key: 615-50.00 mk: 50.00 sn: 615 item: PB key: 616-10.00 mk: 10.00 sn: 616 item: PB key: 616-2.00 mk: 2.00 sn: 616 item: USED key: 616-96.00 mk: 96.00 sn: 616 item: MNH

    tested on solaris something or other.

    I noticed in the original code that the printing statement was in the wrong place, which is most of why the output errors happened.

    --hackmare.