in reply to Simple Array
The value of each key is the numer of entries in the list. If you need to iterate over the list, you use something like this:while (<STDIN>) { $some_hash{$_}++; }
If you maintain a seperate list and hash, you have to update both if one changes. With this method, you only need to track one and thus have an easier maintenance job. The drawback is you lose ordering on the list, but since we have user entered data, this ordering may not matter. If necessary, you could try to keep order with the following (untested) code:foreach (keys %some_hash) { ...some code here... }
my $position = 1; while (<STDIN>) { $myhash{$_}{'item'}++; $myhash{$_}{'position'} = $position++ if ! exists $myhash{$_}{'pos +ition'}; }
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just go the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: (Ovid) Re: Simple Array
by geektron (Curate) on Sep 12, 2000 at 02:17 UTC |