Re: Extracting full digits from a range
by bart (Canon) on Aug 03, 2012 at 18:44 UTC
|
You have to
- get the start and end values,
my($min, $max) = /\b([0-9a-f]+):([0-9a-f]+)\b/i;
- convert them to integers
foreach($min, $max) {
$_ = hex($_);
}
- turn them into a range or span:
my @list = $min .. $max;
- turn each into a hex number.
my @hex = map { sprintf '%X', $_ } $min .. $max;
will do.
- print out the list with ',' between them:
$, = ','; print @list;
update
Oops, that should have been
$, = ','; print @hex;
| [reply] [d/l] [select] |
|
Did you test? It outputs
10559,10560,10561,10562,10563,10564,10565
| [reply] [d/l] |
|
Yeah, I printed the wrong list.
| [reply] |
Re: Extracting full digits from a range
by davido (Cardinal) on Aug 03, 2012 at 23:28 UTC
|
my $string = "293F:2945";
my ( $low, $high ) = unpack( 'nn', pack( 'H4H4', unpack( 'A4xA4', $str
+ing ) ) );
print "$_ " for unpack( '(H4)*', pack( 'n*', $low .. $high ) );
The output:
293f 2940 2941 2942 2943 2944 2945
Let's put it in a (debatably) well-named subroutine so we remember what this gem does:
sub enum_hex_range_from_string {
my ( $low, $high )
= unpack( 'nn', pack( 'H4H4', unpack( 'A4xA4', $string ) ) );
return unpack( '(H4)*', pack( 'n*', $low .. $high ) );
}
Just add some camel casing randomly through the subroutine name and it would feel right at home in a PHP application.
The hardest part of coming up with something like this is keeping track of which of the many pack/unpack template tokens to use from [Hh] and [SsNnVv]. I have to admit to needing to experiment with a couple of one-liners to make sure my conversions were going the right direction.
| [reply] [d/l] [select] |
Re: Extracting full digits from a range
by AnomalousMonk (Archbishop) on Aug 03, 2012 at 18:50 UTC
|
>perl -wMstrict -le
"my $s = 'xxx 293F:2945 yy ff:103 zz';
;;
my @ranges =
map { eval join '..', map hex, split /:/ }
$s =~ m{ [[:xdigit:]]+ : [[:xdigit:]]+ }xmsg
;
;;
printf q{%x }, $_ for @ranges;
"
293f 2940 2941 2942 2943 2944 2945 ff 100 101 102 103
| [reply] [d/l] |
Re: Extracting full digits from a range
by BrowserUk (Patriarch) on Aug 04, 2012 at 00:39 UTC
|
'293F:2945' =~ /([0-9a-f]+):([0-9a-f]+)$/i
and @range = map sprintf( '%x',$_ ), eval "0x$1 .. 0x$2";;
print @range;;
293f 2940 2941 2942 2943 2944 2945
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] [d/l] |
|
Thanks for the help everyone. This one looks like it will fit best into my script, however I'm having one more issue, and it's no doubt something really ridiculous I'm doing wrong.
I have the following code snippet:
#!c:/perl/bin/perl
use strict;
open (HYPERS, "< hypers.txt");
my @hypers = <HYPERS>;
chomp @hypers;
print "Hypers array: @hypers\n";
foreach my $line(@hypers){
$line =~ /([0-9a-f]+):([0-9a-f]+)$/i
and my @range = map sprintf( '%x,',$_ ), eval "0x$1 .. 0x$2";;
print @range;;
}
Output:
Hypers array: 293F:2946
So I know the array is being read, but when trying to shove it into that regex it's coming out as '', almost like it's not there.
The HYPERS file looks like this:
293F:2946
| [reply] [d/l] [select] |
|
C:\test>type junk12.pl
#!c:/perl/bin/perl
use strict;
open (HYPERS, "< hypers.txt");
my @hypers = <HYPERS>;
chomp @hypers;
print "Hypers array: @hypers\n";
foreach my $line(@hypers){
$line =~ /([0-9a-f]+):([0-9a-f]+)$/i
and my @range = map sprintf( '%x,',$_ ), eval "0x$1 .. 0x$2";;
print @range;;
}
C:\test>type hypers.txt
293F:2946
C:\test>junk12
Hypers array: 293F:2946
293f,2940,2941,2942,2943,2944,2945,2946,
C:\test>
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] [d/l] |
|
|
Works for me. Is there any whitespace in the input file (a space at the end of the line)? You migh also suffer from buffering: print a newline after printing the range.
| [reply] |
Re: Extracting full digits from a range
by Kenosis (Priest) on Aug 03, 2012 at 19:56 UTC
|
use Modern::Perl;
'293F:2945' =~ /(.+):(.+)/ and say join ',',
map { sprintf '%X', $_ } eval "0x$1..0x$2";
Output
293F,2940,2941,2942,2943,2944,2945
| [reply] [d/l] [select] |
Re: Extracting full digits from a range
by johngg (Canon) on Aug 03, 2012 at 22:02 UTC
|
knoppix@Microknoppix:~$ perl -Mstrict -Mwarnings -E '
> say join q{,},
> map { sprintf q{%X}, $_ }
> sub { hex $_[ 0 ] .. hex $_[ 1 ] }->
> ( split m{:}, q{293F:2945} );'
293F,2940,2941,2942,2943,2944,2945
knoppix@Microknoppix:~$
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |