home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5553 < prev    next >
Encoding:
Text File  |  1992-08-27  |  1.7 KB  |  43 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!sgi!wdl1!wdl39!mab
  3. From: mab@wdl39.wdl.loral.com (Mark A Biggar)
  4. Subject: Re: easy question?: substrings
  5. Message-ID: <1992Aug27.174613.3432@wdl.loral.com>
  6. Sender: news@wdl.loral.com
  7. Organization: Loral Western Development Labs
  8. References: <1992Aug26.213516.22813@cbnewse.cb.att.com> <1992Aug26.222821.28341@news.eng.convex.com> <BtM69K.54J@world.std.com>
  9. Distribution: usa
  10. Date: Thu, 27 Aug 1992 17:46:13 GMT
  11. Lines: 30
  12.  
  13. In article <BtM69K.54J@world.std.com> dsb@world.std.com (David Boyce) writes:
  14. >In article <1992Aug26.222821.28341@news.eng.convex.com> tchrist@convex.COM (Tom Christiansen) writes:
  15. >>From the keyboard of budnick@cbnewse.cb.att.com (Mary Jo Budnick):
  16. >>:Hi!  I just started to use Perl and can't find the info in the
  17. >>:Wall/Schwartz book very easily to solve my problem.  Help me out?
  18. >>:All I want to do is to change a string in the format of either
  19. >>:    0xffffff00 or ffffff00 into the format
  20. >>:    of 255.255.255.0 
  21. >>Try:
  22. >>    $n = "ffffff00";
  23. >>    print join(".", unpack(C4, pack(L, hex($n)))), "\n";
  24. >>Add quotes around the format strings for old versions of perl.
  25. >    Or what I find less obfuscated:
  26. >$n = "0xffffff00";
  27. >($junk, $f1, $f2, $f3, $f4) = $n=~/(0x|)(..)(..)(..)(..)/;
  28. >printf "%d.%d.%d.%d\n", hex($f1), hex($f2), hex($f3), hex($f4);
  29.  
  30. It doesn't matter as much in this pattern but the use of | in () in very 
  31. ineffecient, that pattern should be written /(0x)?(..)(..)(..)(..)/
  32. In addition there is no reason to do the array assignment so the final version
  33. is:
  34.  
  35. $n =~ /(0x)?(..)(..)(..)(..)/;
  36. printf "%d.%d.%d.%d", hex($2), hex($3), hex($4), hex($5);
  37.  
  38. --
  39. Perl's Maternal Uncle
  40. Mark BIggar
  41. mab@wdl1.wdl.loral.com
  42.  
  43.