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