csebe has asked for the wisdom of the Perl Monks concerning the following question:
And I need to merge the strings as to get:$str1 = "12345 ABC 987 MNO"; $str2 = " CDE";
$str = "12345 CDE 987 MNO";
So, kind of a XOR at a character level if you want.
Being looong strings and maaaany in the files I am processing, I look for your wise input to make it as time efficient as possible, much more than the "obvious" solution:Thanks everyone,my @arr1 = split //, $str1; my @arr2 = split //, $str2; foreach (my $i=0;$i<@arr1;$i++) { if ($arr2[$i]) { # we do have something in the second string if (($arr1[$i] ne " ") and ($arr2[$i] eq " ")) { $arr2[$i] = $arr1[$i]; } } else { #$str2 is empty now, so copy all $arr2[$i] = $arr1[$i]; } } print join "",@arr2,"\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: merging strings (xor at char level?)
by moritz (Cardinal) on Jul 03, 2014 at 08:09 UTC | |
|
Re: merging strings (xor at char level?)
by davido (Cardinal) on Jul 03, 2014 at 07:55 UTC | |
|
Re: merging strings (xor at char level?)
by davido (Cardinal) on Jul 03, 2014 at 16:01 UTC | |
by csebe (Initiate) on Feb 27, 2015 at 11:06 UTC | |
by choroba (Cardinal) on Feb 28, 2015 at 12:31 UTC | |
|
Re: merging strings (xor at char level?)
by perlfan (Parson) on Jul 03, 2014 at 11:39 UTC |