Re: 1 string from 3, making replacements more perlish



On 30 Aug 2006 08:05:54 GMT, anno4000@xxxxxxxxxxxxxxxxxxxxxx wrote:

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 need to create a 4th where the 0's are successively pulled from 2,
sorry Paul, bet it was confusing.
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

Thanks for all the help. I hope to get a better grasp on perl someday.
It's also fun to see all the different ways people think, and the
various ways to do the same thing.

I guess I should have been a little more clear for those interested.
I'm creating string1 first,in a loop made up of X zeros and Y ones,
and it's much longer than this example. Then I'm creating sting2 in a
loop, string 3 in a loop, and they are permutations of smaller subsets
of data. This will allow me, hopefully, to process string 4.
Otherwise, I would have to create a huge permutation first, and delete
those that don't contain the data I'm looking for.
Stu
.



Relevant Pages

  • Re: can anyone explain the last two lines to me?thanks!
    ... That's all your loop is ... your example above this first while loop will give you the hash ... the string $_ is empty. ... element 'guans', so it returns 'hilia', etc. ...
    (comp.lang.perl.misc)
  • Re: How to get a computed string to act as a re in if statement
    ... The aim is to create a string from values passed to the sub-routine ... and then use that as a if criterion for the push operation. ... This is a loop through hash containing key, value pairs of field, ...
    (perl.beginners)
  • extension_pack
    ... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ...
    (comp.lang.vhdl)
  • extension_pack
    ... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ...
    (comp.lang.vhdl)
  • extension_pack
    ... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ...
    (comp.lang.vhdl)