Re: 1 string from 3, making replacements more perlish



On 08/29/2006 04:55 PM, StuPedaso wrote:
I have 1 string made up of ones and zeros, a 2nd and 3rd of letters and number,
and need to create a 4th where the 1's are successively pulled from 2,
and the 1's from the 3rd.

I can this do this in a QB/VB type way with
$string1="001010110";
$string2="a1bd3";
$string3="0XY0";

$l=0;$m=0;$p=0;
$string4="";

for $i (0..(length $string1)){
$x=substr($string1,$l,1);$l++;
if ($x==1){$string4.=substr($string3,$p,1);$p++;}
else {$string4.=substr($string2,$m,1);$m++;}
}
print $string4;
#a10bXdY03

Not very perlish
Also I don't want to modify srting1, as I will be using it again after
I modify 2 and 3.


I hope this is adequately perlish enough for you :-)

use strict;
use warnings;

sub promote(\@) {
my $array = shift;
$array->[0]++;
$array->[0] = 1 if ($array->[0] >= @{$array});
$array->[$array->[0]];
}

my @string1 = split //, '001010110';
my @string2 = (0, split //, 'a1bd3');
my @string3 = (0, split //, '0XY0');
my @string4 = map { $_ ? promote(@string3) : promote(@string2) } @string1;
print @string4;


__HTH__
.



Relevant Pages