From: jis on
Guys,

I have a string $hex which has lets assume "0012345689abcd"

How can I split them into to an array so that
arr[0]=00 ,arr[1] =12..etc

it works with split command like this to some extent
foreach (split(//, $hex){
$arr[$i]=$_;
$i++;
}

Unfortunately when i read big files of 4MB size it takes
like 10mins before it completes execution. No good.
(i couldnt split it like 00,12 but only like 0,0,1,2)

Then I thought unpack wud be a better idea.
@arr = unpack("H2",$data); or
@arr = unpack("H2*",$data);

But only first element got transferred. ie 00.
$arr[0]=00 and arr[1] undefined.

Any one can help me on this?

thanks,
jis





From: Don Piven on
jis wrote:
> Guys,
>
> I have a string $hex which has lets assume "0012345689abcd"
>
> How can I split them into to an array so that
> arr[0]=00 ,arr[1] =12..etc

while ( $hex =~ /[[:xdigit:]]{2}/g ) { push @arr, $1 }

The "g" flag on the regex tells Perl to do its search from where the
previous search left off, so this will just walk through your string two
characters at a time and relieve you from having to keep track of where
you are in the string and in your array.

The "o" flag may also be useful; check "Regexp Quote-Like Operators" in
perlop for more info.
From: John W. Krahn on
Don Piven wrote:
> jis wrote:
>> Guys,
>>
>> I have a string $hex which has lets assume "0012345689abcd"
>>
>> How can I split them into to an array so that
>> arr[0]=00 ,arr[1] =12..etc
>
> while ( $hex =~ /[[:xdigit:]]{2}/g ) { push @arr, $1 }

No need for a loop:

my @arr = $hex =~ /[[:xdigit:]]{2}/g;

Also, you don't use capturing parentheses in your regular expression so
$1 will always be empty.


> The "g" flag on the regex tells Perl to do its search from where the
> previous search left off, so this will just walk through your string two
> characters at a time and relieve you from having to keep track of where
> you are in the string and in your array.
>
> The "o" flag may also be useful; check "Regexp Quote-Like Operators" in
> perlop for more info.

The /o option would not be useful in this case as there are no variables
in the regular expression to interpolate and in any case modern versions
of perl would not re-interpolate a variable that doesn't change.

perldoc -q /o




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
From: John W. Krahn on
jis wrote:
> Guys,
>
> I have a string $hex which has lets assume "0012345689abcd"
>
> How can I split them into to an array so that
> arr[0]=00 ,arr[1] =12..etc

my @arr = unpack '(a2)*', $hex;



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
From: Ben Morrow on

Quoth Don Piven <spamtrap(a)piven.net>:
>
> The "o" flag may also be useful; check "Regexp Quote-Like Operators" in
> perlop for more info.

The /o flag is rarely useful. Any meaningful use of it is better
replaced with qr//, and if compilation-speed is really a concern it's
best to build the regex as a string and pass it through qr// only once.

Ben