Date: 5 Oct 2001 01:07:38 GMT From: damian@qimr.edu.au (Damian James) Subject: Re: Multiplexing strings Message-Id: On 4 Oct 2001 15:46:21 -0700, Dimitri said: >I have N=3 strings of equal length, and I want to multiplex them by taking >M=2 characters from each string (the length of each string is divisible by >M). Example : > >Input: > >$str1 = "aabbccddeeff"; >$str2 = "AABBCCDDEEFF"; >$str3 = "001122334455"; > >Output : > >$out = "aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55"; > >Besides the obvious for loop : > >$out = ""; $len = length($str1); > >for ($i = 0; $i < $len; $i += 2) { > $out .= substr($str1, $i, 2) . substr($str2, $i, 2) . substr($str3, $i, 2); >} > >Is there a more elegant (faster) way? Don't know about faster, certainly less memory efficient, but in any case a workable Other Way To Do It: #!/usr/bin/perl use warnings; use strict; my @str = qw ( aabbccddeeff AABBCCDDEEFF 001122334455 ); my @seq = map { [ /(..)/g ] } @str; my $out = join '', map { $seq[0]->[$_], $seq[1]->[$_], $seq[2]->[$_] } 0..5; print "$out\n"; I'm sure someone can think of a neater way to handle that map(). Cheers, Damian -- @:=grep!(m!$/|#!..$|),split//,;@;=0..$#:;while($:=@;){$;=rand $:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,, $,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__ Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub