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

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!caen!destroyer!ubc-cs!dhami
  3. From: dhami@cs.ubc.ca (Mandeep S Dhami)
  4. Subject: Re: emulating mulit-dimentional arrays?
  5. Message-ID: <1992Sep9.174452.18421@cs.ubc.ca>
  6. Sender: usenet@cs.ubc.ca (Usenet News)
  7. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  8. References: <1992Sep9.163329.12565@cbnewse.cb.att.com>
  9. Distribution: usa
  10. Date: Wed, 9 Sep 92 17:44:52 GMT
  11. Lines: 62
  12.  
  13. budnick@cbnewse.cb.att.com (Mary Jo Budnick) writes:
  14.  
  15. >        $name = foo;
  16. >        $index = 0;
  17. >        $time{$name, $index} = "11:00:00";
  18. ...
  19. >Now, how do I later retrieve all of the names and process all of
  20. >the times for that name?
  21. >If I do:
  22. >        foreach $i (keys(%time)) {
  23. >                print "key = $i, time = $time{$key}\n";
  24. >        }
  25. >I get:
  26. >        key = foo1
  27. >
  28. ...
  29. >What I want to do is get "foo" and then get all of the times for
  30. >foo, indexed by 0,1, . . .  
  31. ...
  32.  
  33. try something like:
  34.  
  35. $name=foo;
  36. $index=0;
  37. $time{"$name:$index"} = "11:00:00";
  38. ...
  39.  
  40. and then:
  41. foreach (sort(keys(%time))) {
  42.   split(/:/);
  43.   print "name=$_[0] index=$_[1] time=$time{$_}\n";
  44. }
  45.  
  46. Or to get foo only once try:
  47. $last='';
  48. foreach (sort(keys(%time))) {
  49.   split(/:/);
  50.   ($last ne $_[0]) && print "Name ",$last=$_[0],":\n";
  51.   print "\t index=$_[1] time=$time{$_}\n";
  52. }
  53.  
  54. Or you may use aliasing by local(*array) to make it quicker.
  55. (See FAQ for an example).
  56.  
  57. This will be sorted. This, of course, assumes that : will never occur
  58. in keys, you can use something like \377 otherwise. OR You may use ' '
  59. and split; instead ... (perl you know)
  60.  
  61. Mandeep
  62. __________________________________________________________________
  63. "I'm very brave generally," he went on in a low voice:
  64. "only to-day I happen to have a headache."
  65.                                       -- Lewis Carroll
  66. __________________________________________________________________
  67.