Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split every other character? 3

Status
Not open for further replies.

serpento

Programmer
Jun 16, 2002
92
GB
I know how to split a scalar every character:
Code:
  $scalar = 'AABBCC';
  @list = split('', $scalar);
But how can a split it every OTHER character, so
Code:
@list = ('AA', 'BB', 'CC')
?

-Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
Look up unpack
Code:
$file="AABBCCDD";
@list= unpack "A2 A2 A2 A2", $file;
foreach $item (@list) {
	print "$item\n";
}
HTH
Paul
 
Woooaah that was quick.
And seems to work too.
Thanks.

-Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
TopMach,

Your regex works and is slightly more efficient over a 4 character string, but it gets less so over longer strings
Code:
#!/usr/bin/perl
# AABB 28 Vs 25
# AABBCCDD 27 Vs 38
# AABBCCDDAABBCCDDAABBCCDD 29 Vs 92

my $i = 0;
 $start=time;
 for ($i=0;$i<2000001;$i++) {
   $file=&quot;AABBCCDDAABBCCDDAABBCCDD&quot;;
   @list= unpack &quot;A2 A2 A2 A2&quot;, $file;
}
 $finish=time;
 $taken=$finish-$start;
print &quot;unpack ->>$taken \n&quot;;

my $start=time;
for ($i=0;$i<2000001;$i++) {
   $file=&quot;AABBCCDDAABBCCDDAABBCCDD&quot;;
   @list= $file =~ /(..)/sg;
}
$finish=time;
$taken=$finish-$start;
print &quot;Regex->>$taken\n&quot;;
Activestate Perl, PIII500, Win98SE

Regards
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Paul, maybe because your unpack function only unpacks the first 8 characters?

$file = &quot;AABBCCDDAABBCCDDAABBCCDD&quot;;
@list = unpack &quot;A2 A2 A2 A2&quot;, $file;
print &quot;@list&quot;;

# prints AA BB CC DD

$file = &quot;AABBCCDDAABBCCDDAABBCCDD&quot;;
@list = $file =~ /(..)/gs;
print &quot;@list&quot;;

# prints AA BB CC DD AA BB CC DD AA BB CC DD

I think using unpack was a good idea though. The only problem is that the template you gave is only good for 4 x 2 chars. I've did some benchmarking on a file 80 chars per lines and 2000 lines with a modified template and my regex. There is very little difference but the unpack is faster. for 100 iterations unpack method took 43 seconds and the regex took 46 seconds. The template I used for the unpack, which is not limited, is:

@list = unpack &quot;(A2)*&quot;, $file;

So yes unpack is faster than my regex. But your benchmark wasn't fair as it only made 4 pairs compared to the regex which took the entire string.

A tip for benchmarking. It's better to isolate code that you want to compare. In the above code you gave you set the variable $file on each iteration. The problem is that you can't really see the factor ratio difference that way. For instance, if setting a variable would take 1 second but the actual code that operate on it would take 1 millisecond on one test and 1.5 millisecond on the other test it would be hard to tell by how much a solution is better than the other.

You've got to explain why you use an odd number like 2000001 and not a round number. It really intrigues me.
 
TopMach,
2000001 is over 2 million iterations. x+1 (I'm thinking of going into marketing .. the dark side) NFW

To be fair, I ran different versions of the code that I didn't put up ... point made á TM, and the results were unfair, upon reflection. A goal ...

I take your point on the comparisons though, and I should have generated the unpack template dynamically based on the length of the string ... mea culpa

The reason for the X+1 iterations was supposed to be that each test was submitted to the same criteria within the loop (which WE know weren't)

But...
It's late, * to TM, for a very reasonable argument
Best Regards
Paul


I could come back on this one ... but mebbee not ...


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top