transposing %d values to %x output

Hi there,

s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1:$2:$3:$4::0/

so is there a slick, easily readable way to get the value $1, $2, $3,
$4 to be rewriten as %x instead of a %d?


Cheers,

Noah


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Noah Garrett Wallach [ Mi, 30 März 2011 01:50 ] [ ID #2057356 ]

Re: transposing %d values to %x output

On 11-03-29 07:50 PM, Noah Garrett Wallach wrote:
> Hi there,
>
> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1:$2:$3:$4::0/
>
> so is there a slick, easily readable way to get the value $1, $2, $3, $4
> to be rewriten as %x instead of a %d?

s/(\d+)\.(\d+)\.(\d+)\.(\d+)/sprintf('%x:%x:%x:%x::0',$1,$2, $3,$4)/e;

See:
`perldoc -f sprintf`
`perldoc perlre` and search for /\/e/


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Mi, 30 März 2011 01:56 ] [ ID #2057357 ]

Re: transposing %d values to %x output

On 3/29/11 Tue Mar 29, 2011 4:50 PM, "Noah Garrett Wallach"
<noah-list [at] enabled.com> scribbled:

> Hi there,
>
> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1:$2:$3:$4::0/
>
> so is there a slick, easily readable way to get the value $1, $2, $3,
> $4 to be rewriten as %x instead of a %d?

Try this:

s/(\d+)\.(\d+)\.(\d+)\.(\d+)/sprintf('%x:%x:%x:%x::0',$1,$2, $3,$4/e;



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Mi, 30 März 2011 01:58 ] [ ID #2057358 ]

Re: transposing %d values to %x output

On Tue, Mar 29, 2011 at 19:50, Noah Garrett Wallach
<noah-list [at] enabled.com> wrote:
snip
> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1:$2:$3:$4::0/
>
> so is there a slick, easily readable way =C2=A0to get the value $1, $2, $=
3, $4 to
> be rewriten as =C2=A0%x instead of a %d?
snip

The [eval regex modifier (e)][0] will let you use an arbitrary
expression for the replacement. You can then use [sprintf][1] to
print the captures in hexadecimal. It is important to note that \d
doesn't match what you think it does. Starting with Perl 5.8, \d
matches and digit character. This includes characters such as
"\x{1815}" (Mongolian digit five). To match the ASCII digit
characters you must use [0-9]:

s/([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)/sprintf
"%02x:%02x:%02x:%02x::0", $1, $2, $3, $4/e;

It is also important to note that, if this is supposed to match an IP
Address, it will match invalid strings like "256.256.256.256". If you
only want to match numbers between 0 and 255, it can be done using a
regex like this:

my $octet =3D "(?:[1-9]?0-9|1[0-9]{2}|2[1-4][0-9]|25[0-5])";
s/\b($octet)[.]($octet)[.]($octet)[.]($octet)\b/sprintf
"%02x:%02x:%02x:%02x::0", $1, $2, $3, $4/e;

[0]: http://perldoc.perl.org/perlop.html#s%2fPATTERN%2fREPLACEMEN T%2fmsixpo=
gce
[1]: http://perldoc.perl.org/functions/sprintf.html
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Mi, 30 März 2011 03:18 ] [ ID #2057359 ]

Re: transposing %d values to %x output

At 9:18 PM -0400 3/29/11, Chas. Owens wrote:

>It is important to note that \d
>doesn't match what you think it does. Starting with Perl 5.8, \d
>matches and digit character. This includes characters such as
>"\x{1815}" (Mongolian digit five). To match the ASCII digit
>characters you must use [0-9]:

I have heard this advice before, and it just sounds silly to me. I
deal exclusively with ASCII characters, so \d will only match the
characters '0' through '9'. If any UTF characters have crept into my
data unknowingly, then I have a bigger problem than too many matches.
If I am dealing with Monogolian characters or with any other set of
UTC characters, then I certainly want \d to match them as well. There
is a good reason why the set of characters matched by \d was expanded.

In other words, I see no reason to avoid using \d. Advising beginners
to avoid the use of \d is just plain wrong. Can you provide any
example of a situation where something bad will happen from using \d?

The statement "To match the ASCII digit characters you must use
[0-9]" is wrong. I believe you meant to say "To match the ASCII digit
characters and only those characters, you must use [0-9]".


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Mi, 30 März 2011 04:03 ] [ ID #2057360 ]

Re: transposing %d values to %x output

--0016e6d97651751b76049fa9ee1e
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Mar 29, 2011 at 11:03 PM, Jim Gibson <jimsgibson [at] gmail.com> wrote:
>
> The statement "To match the ASCII digit characters you must use [0-9]" is
> wrong. I believe you meant to say "To match the ASCII digit characters and
> only those characters, you must use [0-9]".


Or \p{PosixDigit}; Teach new people Unicode character properties! They'll
hate/love you forever. Or (?a:\d) if they are running blead or some of the
latest releases in the 5.13 series!

Sorry, that "you must use" got a bit under my nails ; ) That aside though, I
agree with most of what you say. Warning beginners of the pitfalls of \d and
friends, however, is the way to go: I live in a country where ASCII doesn't
make up the entire character set, but everyone in my office seems to think
that it does. I constantly wish someone would've taught them better whenever
they started programming*.

Brian.

*(it's a Ruby job though, which makes things even more painful)

--0016e6d97651751b76049fa9ee1e--
Brian Fraser [ Mi, 30 März 2011 04:28 ] [ ID #2057361 ]

Re: transposing %d values to %x output

On Tue, Mar 29, 2011 at 22:03, Jim Gibson <jimsgibson [at] gmail.com> wrote:
snip
> I have heard this advice before, and it just sounds silly to me.
> I deal exclusively with ASCII characters, so \d will only match
> the characters '0' through '9'.
snip

If you are dealing exclusively with ASCII, then you should be using
the [bytes][0] pragma; however, just because you are dealing with only
ASCII today doesn't mean you will always be dealing only with ASCII.
At some point in the near future you will probably have to start
dealing with Unicode. When that happens (and it really is a matter of
when, not if), do you want to go through all of your code and fix the
problems you could have easily avoided?

snip
> Can you provide any example of a situation where something bad
> will happen from using \d?
snip

Simple. In the case we are talking about things will got terribly
astray if you happened to match something that wasn't [0-9]:

#!/usr/bin/perl

use strict;
use warnings;

binmode STDOUT, ":utf8";

my $s =3D "\x{ff15}.\x{ff15}.\x{ff15}.\x{ff15}";

print "$s\n";
$s =3D~ s/(\d+)[.](\d+)[.](\d+)[.](\d+)/sprintf "%x.%x.%x.%x", $1, $2, $3, =
$4/e;
print "$s\n";

=EF=BC=95.=EF=BC=95.=EF=BC=95.=EF=BC=95
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
0.0.0.0

The problem is that things that match \d just don't behave like
numbers. Only ten of the hundreds of digits characters are actually
numbers. They may look like numbers, as the Fullwidth Digit Five
characters I used here do, but Perl 5 still treats them like
non-number characters.

snip
> The statement "To match the ASCII digit characters you must use [0-9]" is
> wrong. I believe you meant to say "To match the ASCII digit characters an=
d
> only those characters, you must use [0-9]".
snip

By your logic, it is perfectly fine to use . to match numbers. After
all, . will match [0-9]. If you have bothered to specify \d, you most
likely mean [0-9].

[0]: http://perldoc.perl.org/bytes.html

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Mi, 30 März 2011 05:46 ] [ ID #2057362 ]

Re: transposing %d values to %x output

--0016e6dd8f444e8979049fab9d56
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Mar 30, 2011 at 12:46 AM, Chas. Owens <chas.owens [at] gmail.com> wrote:

>
> If you are dealing exclusively with ASCII, then you should be using
> the [bytes][0] pragma;


It's nitpicky, but I'd advice against ever recommending use bytes in the
beginners list. Or any list really. See:
http://www.nntp.perl.org/group/perl.perl5.porters/2011/03/ms g170010.html

Brian.

--0016e6dd8f444e8979049fab9d56--
Brian Fraser [ Mi, 30 März 2011 06:28 ] [ ID #2057363 ]

Re: transposing %d values to %x output

Shawn H Corey wrote:
> On 11-03-29 07:50 PM, Noah Garrett Wallach wrote:
>>
>> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1:$2:$3:$4::0/
>>
>> so is there a slick, easily readable way to get the value $1, $2, $3, $4
>> to be rewriten as %x instead of a %d?
>
> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/sprintf('%x:%x:%x:%x::0',$1,$2, $3,$4)/e;

Might be better as:

sprintf '%x:%x:%x:%x::0', /(\d+)\.(\d+)\.(\d+)\.(\d+)/;

So you don't have to use eval.


John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Mi, 30 März 2011 08:03 ] [ ID #2057364 ]

Re: transposing %d values to %x output

>>>>> "JWK" == John W Krahn <jwkrahn [at] shaw.ca> writes:

JWK> Shawn H Corey wrote:
>> On 11-03-29 07:50 PM, Noah Garrett Wallach wrote:
>>>
>>> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1:$2:$3:$4::0/
>>>
>>> so is there a slick, easily readable way to get the value $1, $2, $3, $4
>>> to be rewriten as %x instead of a %d?
>>
>> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/sprintf('%x:%x:%x:%x::0',$1,$2, $3,$4)/e;

JWK> Might be better as:

JWK> sprintf '%x:%x:%x:%x::0', /(\d+)\.(\d+)\.(\d+)\.(\d+)/;

JWK> So you don't have to use eval.

that works too but don't confuse /e with string eval. in /e the code is
compiled at compile time, not runtime so it is safe and you can't do
anything more dangerous as regular code could. on the otherhand, /ee
(which does work by accident of larry! :) does call string eval on the
result of the replacment expression.

the other difference is that the s///e version replaces the IP with a
hex version and yours needs to be assigned back to the variable. not a
big diff.

and if you are only working on an ip address with no other text, you can
even reduce yours (and the others) to a single (\d+) with /g:

sprintf '%x:%x:%x:%x::0', /(\d+)/g ;

the s/// could be rewritten like this (note both /e and /g are used):

$ip =~ s/(\d+)\.?/sprintf('%x:',$1)/eg ;
$ip .= ':0' ;

here is a working example:

perl -le '$x ="12.23.34.56"; $x =~ s/(\d+)\.?/sprintf("%x:",$1)/eg; print $x'
c:17:22:38:

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Mi, 30 März 2011 08:15 ] [ ID #2057365 ]

Re: transposing %d values to %x output

Uri Guttman wrote:
>>>>>> "JWK" == John W Krahn<jwkrahn [at] shaw.ca> writes:
>
> JWK> Shawn H Corey wrote:
> >> On 11-03-29 07:50 PM, Noah Garrett Wallach wrote:
> >>>
> >>> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1:$2:$3:$4::0/
> >>>
> >>> so is there a slick, easily readable way to get the value $1, $2, $3, $4
> >>> to be rewriten as %x instead of a %d?
> >>
> >> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/sprintf('%x:%x:%x:%x::0',$1,$2, $3,$4)/e;
>
> JWK> Might be better as:
>
> JWK> sprintf '%x:%x:%x:%x::0', /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
>
> JWK> So you don't have to use eval.
>
> that works too but don't confuse /e with string eval. in /e the code is
> compiled at compile time, not runtime so it is safe and you can't do
> anything more dangerous as regular code could. on the otherhand, /ee
> (which does work by accident of larry! :) does call string eval on the
> result of the replacment expression.
>
> the other difference is that the s///e version replaces the IP with a
> hex version and yours needs to be assigned back to the variable. not a
> big diff.
>
> and if you are only working on an ip address with no other text, you can
> even reduce yours (and the others) to a single (\d+) with /g:
>
> sprintf '%x:%x:%x:%x::0', /(\d+)/g ;

And if you do it like that you don't even need the capturing parentheses:

sprintf '%x:%x:%x:%x::0', /\d+/g ;


> the s/// could be rewritten like this (note both /e and /g are used):
>
> $ip =~ s/(\d+)\.?/sprintf('%x:',$1)/eg ;
> $ip .= ':0' ;
>
> here is a working example:
>
> perl -le '$x ="12.23.34.56"; $x =~ s/(\d+)\.?/sprintf("%x:",$1)/eg; print $x'
> c:17:22:38:



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Mi, 30 März 2011 08:24 ] [ ID #2057366 ]

Re: transposing %d values to %x output

From: Jim Gibson <jimsgibson [at] gmail.com>
> At 9:18 PM -0400 3/29/11, Chas. Owens wrote:
>
> >It is important to note that \d
> >doesn't match what you think it does. Starting with Perl 5.8, \d
> >matches and digit character. This includes characters such as
> >"\x{1815}" (Mongolian digit five). To match the ASCII digit
> >characters you must use [0-9]:
>
> I have heard this advice before, and it just sounds silly to me. I
> deal exclusively with ASCII characters, so \d will only match the
> characters '0' through '9'. If any UTF characters have crept into my
> data unknowingly, then I have a bigger problem than too many matches.
> If I am dealing with Monogolian characters or with any other set of
> UTC characters, then I certainly want \d to match them as well. There
> is a good reason why the set of characters matched by \d was expanded.

The \d had been rendered useless. \d had meant
"something I can do math with" for too long to be changed to "something
someone might consider a digit". Which if it was to be correct would include
the roman numerals "I, V, X, L, C, D, M, ..."

Hey, it doesn't, \d is wrong even according to the silly new definition!

The "anything that might be considered a digit in Unicode" they should have used some \N{} or \p{} and leave the \d alone.

Yes, if you happened to want to match the mongolian digit thirteen
you would have to use \p{IsDigit} or something. Huge deal.

Instead of forcing those few and far apart that need (for whatever
reason that completely alludes me) to match anything resembling a
digit to use the Unicode \p{} construct or something similar,
everyone had been forced to change their code to prevent their
regexps suddenly matching something they never meant to match and
that the computer will have no use for. So much for backwards
compatibility.

Jenda
===== Jenda [at] Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jenda Krynicky [ Mi, 30 März 2011 11:46 ] [ ID #2057367 ]

Re: transposing %d values to %x output

On Wed, Mar 30, 2011 at 00:28, Brian Fraser <fraserbn [at] gmail.com> wrote:
> On Wed, Mar 30, 2011 at 12:46 AM, Chas. Owens <chas.owens [at] gmail.com> wrot=
e:
>>
>> If you are dealing exclusively with ASCII, then you should be using
>> =C2=A0the [bytes][0] pragma;
>
> It's nitpicky, but I'd advice against ever recommending use bytes in the
> beginners list. Or any list really. See:
> http://www.nntp.perl.org/group/perl.perl5.porters/2011/03/ms g170010.html
> Brian.

Hmm, I had missed that change. Thanks. An easier to read version
comes from the Perl 5.13.11 version of the bytes pod:

This pragma reflects early attempts to incorporate Unicode into perl and
has since been superseded. It breaks encapsulation (i.e. it exposes the
innards of how the perl executable currently happens to store a string),
and use of this module for anything other than debugging purposes is
strongly discouraged. If you feel that the functions here within might be
useful for your application, this possibly indicates a mismatch between
your mental model of Perl Unicode and the current reality. In that case,
you may wish to read some of the perl Unicode documentation:
L<perluniintro>, L<perlunitut>, L<perlunifaq> and L<perlunicode>.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Mi, 30 März 2011 13:53 ] [ ID #2057369 ]
Perl » gmane.comp.lang.perl.beginners » transposing %d values to %x output

Vorheriges Thema: How to not hard code <STDIN>?
Nächstes Thema: problem with naming of variables