home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!elroy.jpl.nasa.gov!ucla-cs!cognet.ucla.edu!dwells
- From: dwells@cognet.ucla.edu (David Wells)
- Subject: Re: emulating mulit-dimentional arrays?
- Message-ID: <1992Sep10.004706.26168@cognet.ucla.edu>
- Sender: news@cognet.ucla.edu
- Organization: UCLA Cognitive Science Program
- References: <1992Sep9.163329.12565@cbnewse.cb.att.com>
- Distribution: usa
- Date: Thu, 10 Sep 1992 00:47:06 GMT
- Lines: 46
-
- In article <1992Sep9.163329.12565@cbnewse.cb.att.com> budnick@cbnewse.cb.att.com (Mary Jo Budnick) writes:
- >I see there are no multi-dimensional arrays in Perl; however,
- >they can be emulated w/ join and "$;". So, say I want to keep
- >track of "time" specific to a name and a counter:
- >
- > $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?
- >
- >What I want to do is get "foo" and then get all of the times for
- >foo, indexed by 0,1, . . . Then I want to get "goo" and get
- >the times for it indexed by 0,1,. . . I don't know the total
- >number of names or what they are. I don't know the total
- >number of times (indices) for each name.
-
- One way would be to build a list of times for each name, and then keep
- an associative array of names. Such as:
-
- $name = foo;
- $nameList{$name}++;
- eval "push(@${name}, '11:00:00')";
-
- Then to get all the times for all names:
-
- foreach $name (keys %nameList) {
- print "$name: ";
- foreach $value (eval "@${name}") ) {
- print "$value ";
- }
- print "\n";
- }
-
- This may be a sideways way to do this (I'm sure someone will let me
- know :-) but it is interesting, anyway...
-
-
- David
-
- --
- David J Wells
- "Cooking hotdogs with napalm" w310/206-3960
- - The Dead Milkmen dwells@cognet.ucla.edu
-