home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5346 < prev    next >
Encoding:
Internet Message Format  |  1992-08-17  |  1.5 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!mips!sdd.hp.com!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Assoc array indirection
  5. Message-ID: <1992Aug17.170850.13766@netlabs.com>
  6. Date: 17 Aug 92 17:08:50 GMT
  7. References: <1992Aug17.100513.9649@nuscc.nus.sg>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 40
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <1992Aug17.100513.9649@nuscc.nus.sg> ccechk@nuscc.nus.sg (Heng Kek) writes:
  14. : #!/usr/bin/perl
  15. :   %ipaddr = ('nusunix1','137.132.73.11',
  16. :              'nusunix2','137.132.10.11',
  17. :              'nusunix3','137.132.91.12'
  18. :             );
  19. :   @host   = ('nusunix1',
  20. :              'nusunix2',
  21. :              'nusunix3'
  22. :             );
  23. : print eval "\@ipaddr{@host[0..1]}";              # no go
  24. : print eval "\@ipaddr{join(',', @host[0..1])}";  # no go
  25. : print eval "\@ipaddr{$host[0], $host[1]}";      # okay
  26.  
  27. This only works accidentally because all your host names happen to
  28. be identifiers, which when parsed are interpreted as unquoted strings.
  29.  
  30. : Why doesn't the 1st 2 print()s do what I expect?
  31.  
  32. Because you aren't interpolating a comma between the two host names.
  33.  
  34. : How do I achieve my goal, given my intentions in:
  35. : print eval "\@ipaddr{join(',', @host[0..1])}";  # no go
  36.  
  37. Why do you quote the first @ and not the second?  Try
  38.  
  39.     print eval "\@ipaddr{\@host[0..1]}";
  40.  
  41. or better
  42.  
  43.     print eval '@ipaddr{@host[0..1]}';
  44.  
  45. or even better
  46.  
  47.     print @ipaddr{@host[0..1]};
  48.  
  49. Why do you need the eval?
  50.  
  51. Larry
  52.