home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!caen!destroyer!ubc-cs!dhami
- From: dhami@cs.ubc.ca (Mandeep S Dhami)
- Subject: Re: emulating mulit-dimentional arrays?
- Message-ID: <1992Sep9.174452.18421@cs.ubc.ca>
- Sender: usenet@cs.ubc.ca (Usenet News)
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- References: <1992Sep9.163329.12565@cbnewse.cb.att.com>
- Distribution: usa
- Date: Wed, 9 Sep 92 17:44:52 GMT
- Lines: 62
-
- budnick@cbnewse.cb.att.com (Mary Jo Budnick) writes:
-
- >
- > $name = foo;
- > $index = 0;
- > $time{$name, $index} = "11:00:00";
- >
- >
- ...
- >Now, how do I later retrieve all of the names and process all of
- >the times for that name?
- >
- >If I do:
- >
- > foreach $i (keys(%time)) {
- > print "key = $i, time = $time{$key}\n";
- > }
- >
- >
- >I get:
- >
- > key = foo1
- >
- ...
- >What I want to do is get "foo" and then get all of the times for
- >foo, indexed by 0,1, . . .
- ...
-
- try something like:
-
- $name=foo;
- $index=0;
- $time{"$name:$index"} = "11:00:00";
- ...
-
- and then:
- foreach (sort(keys(%time))) {
- split(/:/);
- print "name=$_[0] index=$_[1] time=$time{$_}\n";
- }
-
- Or to get foo only once try:
- $last='';
- foreach (sort(keys(%time))) {
- split(/:/);
- ($last ne $_[0]) && print "Name ",$last=$_[0],":\n";
- print "\t index=$_[1] time=$time{$_}\n";
- }
-
- Or you may use aliasing by local(*array) to make it quicker.
- (See FAQ for an example).
-
- This will be sorted. This, of course, assumes that : will never occur
- in keys, you can use something like \377 otherwise. OR You may use ' '
- and split; instead ... (perl you know)
-
- Mandeep
- __________________________________________________________________
- "I'm very brave generally," he went on in a low voice:
- "only to-day I happen to have a headache."
- -- Lewis Carroll
- __________________________________________________________________
-