home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5824 < prev    next >
Encoding:
Text File  |  1992-09-09  |  1.8 KB  |  59 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!ucla-cs!cognet.ucla.edu!dwells
  3. From: dwells@cognet.ucla.edu (David Wells)
  4. Subject: Re: emulating mulit-dimentional arrays?
  5. Message-ID: <1992Sep10.004706.26168@cognet.ucla.edu>
  6. Sender: news@cognet.ucla.edu
  7. Organization: UCLA Cognitive Science Program
  8. References: <1992Sep9.163329.12565@cbnewse.cb.att.com>
  9. Distribution: usa
  10. Date: Thu, 10 Sep 1992 00:47:06 GMT
  11. Lines: 46
  12.  
  13. In article <1992Sep9.163329.12565@cbnewse.cb.att.com> budnick@cbnewse.cb.att.com (Mary Jo Budnick) writes:
  14. >I see there are no multi-dimensional arrays in Perl; however,
  15. >they can be emulated w/ join and "$;".  So, say I want to keep
  16. >track of "time" specific to a name and a counter:
  17. >        $name = foo;
  18. >        $index = 0;
  19. >        $time{$name, $index} = "11:00:00";
  20. >      ...
  21. >Now, how do I later retrieve all of the names and process all of
  22. >the times for that name?
  23. >What I want to do is get "foo" and then get all of the times for
  24. >foo, indexed by 0,1, . . .  Then I want to get "goo" and get
  25. >the times for it indexed by 0,1,. . .  I don't know the total
  26. >number of names or what they are.  I don't know the total
  27. >number of times (indices) for each name.
  28.  
  29. One way would be to build a list of times for each name, and then keep
  30. an associative array of names.  Such as:
  31.  
  32.     $name = foo;
  33.     $nameList{$name}++;
  34.     eval "push(@${name}, '11:00:00')";
  35.  
  36. Then to get all the times for all names:
  37.  
  38.     foreach $name (keys %nameList) {
  39.         print "$name: ";
  40.         foreach $value (eval "@${name}") ) {
  41.             print "$value ";
  42.         }
  43.         print "\n";
  44.     }
  45.  
  46. This may be a sideways way to do this (I'm sure someone will let me
  47. know :-) but it is interesting, anyway...
  48.  
  49.  
  50. David
  51.  
  52. -- 
  53.                                 David J Wells
  54. "Cooking hotdogs with napalm"                    w310/206-3960
  55.       - The Dead Milkmen                   dwells@cognet.ucla.edu
  56.