Re: 1 string from 3, making replacements more perlish
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxx
- Date: 30 Aug 2006 08:05:54 GMT
DJ Stunks <DJStunks@xxxxxxxxx> wrote in comp.lang.perl.misc:
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.
what do y'all think of this:
#!/usr/bin/perl
use strict;
use warnings;
my $key_string = '001010110';
my $string_0 = 'a1bd3';
my $string_1 = '0XY0';
my %hash = (
0 => [ split //, $string_0 ],
1 => [ split //, $string_1 ],
);
An array of two elements would do instead of the hash.
my @pair = map [ split //], $string_0, $string_1;
my $result;
for my $i (split //, $key_string) {
$result .= shift @{ $hash{$i} };
}
join() and map() can replace he loop:
my $result = join '', map shift( @{ $pair[ $_] }), split //, $key_string;
(Untested code)
Anno
.
- Follow-Ups:
- Re: 1 string from 3, making replacements more perlish
- From: StuPedaso
- Re: 1 string from 3, making replacements more perlish
- References:
- 1 string from 3, making replacements more perlish
- From: StuPedaso
- Re: 1 string from 3, making replacements more perlish
- From: DJ Stunks
- 1 string from 3, making replacements more perlish
- Prev by Date: Re: FormMail Error Bad/No Recipient, Browser Issue
- Next by Date: Re: Stupid Q: How to preserve numeric characters
- Previous by thread: Re: 1 string from 3, making replacements more perlish
- Next by thread: Re: 1 string from 3, making replacements more perlish
- Index(es):
Relevant Pages
|