Re: 1 string from 3, making replacements more perlish



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
.



Relevant Pages

  • Re: 1 string from 3, making replacements more perlish
    ... StuPedaso wrote: ... a 2nd and 3rd of letters and number, ... Also I don't want to modify srting1, as I will be using it again after ... my %hash = ( ...
    (comp.lang.perl.misc)
  • Re: Cross-language challenge (of POSSIBLE interest)
    ... For example the program may be coded as a simple array or as a pointer ... dynamic structure. ... Or it could be done using a hash table. ... The absolute fastest would be using a hash using all letters that will ...
    (comp.lang.cobol)
  • Re: reversing hash ?
    ... I have found one web site out there that uses a very similar script to ... digit of the second hash from the "encrypted" (I really don't want to use ... the first character of both are changed by the same ... For example, the first URL is 6 letters long, so ...
    (sci.crypt)
  • Re: combinations listing
    ... It splits every letter of one word, sorts the letters and joins them ... now for acb: ... Hope you see how the hash is built. ...
    (comp.lang.ruby)
  • Re: Letter of claim - p2p
    ... recieve the required order. ... Cynic's posts outline an interesting scenario, ... individuals who have recieved letters fromn Davenport. ... If I can determine the actual hash value of this file, ...
    (uk.legal)