Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

concatenating elements of an array in twos

by Sandy_Bio_Perl (Beadle)
on Aug 16, 2016 at 22:58 UTC ( [id://1169870]=perlquestion: print w/replies, xml ) Need Help??

Sandy_Bio_Perl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Brethren. I am trying to combine two elements of an array at a time. (Please see my data below) The result I am trying to achieve is sid 1|FLPSDFFPS LLWFHISCL WIRTPPAYR YVNVNMGLK, sid 2|FLPSDFFPS LLWFHISCL FLPSDFFPS ELMNLATWV etc.

I have tried the following code, but it doesnt concatenate the peptides

foreach my $sid (@sid){ foreach my $el (@unpairedSidBinder){ my @sidtwobinder = split /\|/,$el; #print "\nline192 sid=$sid twobinder[0]=$sidtwobinder[0] twobi +nder[1]=$sidtwobinder[1]n"; if ($sid eq $sidtwobinder[0]){ my $temp = $sidtwobinder[1]; my $templine .=$temp; push @combinedBinders, $templine; } } }

Any help would be enormously appreciated

--DATA--

@sid = (1,2,7,9) @unpairedSidBinder = (sid 1|FLPSDFFPS LLWFHISCL sid 1|WIRTPPAYR YVNVNMGLK sid 2|FLPSDFFPS LLWFHISCL sid 2|FLPSDFFPS ELMNLATWV sid 7|FLPSDFFPS ATVELLSFL sid 7|VWIRTPPAY LLSFLPSDF sid 9|VWIRTPPAY LLDTASALY sid 9|FGRETVLEY PSDFFPSVR)

Replies are listed 'Best First'.
Re: concatenating elements of an array in twos
by choroba (Cardinal) on Aug 16, 2016 at 23:27 UTC
    $templine starts undefined, but you add $temp to it. Do you want to concatenate $sid and $sidtwobinder[1] ? Also, "sid 1" and "1" are different strings.

    @sid is not needed, you can extract it from the left hand sides of the unpaired.

    Just iterate over the list and check wheter the sid is the same as previously. If yes, concatenate, if not, start a new one.

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @unpairedSidBinder = ( 'sid 1|FLPSDFFPS LLWFHISCL', 'sid 1|WIRTPPAYR YVNVNMGLK', 'sid 2|FLPSDFFPS LLWFHISCL', 'sid 2|FLPSDFFPS ELMNLATWV', 'sid 7|FLPSDFFPS ATVELLSFL', 'sid 7|VWIRTPPAY LLSFLPSDF', 'sid 9|VWIRTPPAY LLDTASALY', 'sid 9|FGRETVLEY PSDFFPSVR', ); my @combined = (); my $last = q(); for my $item (@unpairedSidBinder) { my ($left, $right) = split /\|/, $item; if ($left eq $last) { $combined[-1] .= " $right"; } else { push @combined, "$left|$right"; $last = $left; } } say for @combined;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thank you very much, that worked a treat

Re: concatenating elements of an array in twos
by tangent (Parson) on Aug 16, 2016 at 23:27 UTC
    Where you have
    if ($sid eq $sidtwobinder[0])
    you are not getting a match because $sid is "1" and $sidtwobinder[0] is "sid 1" (note the two spaces between sid and 1). You could change your @sid array to contain the whole text. I don't know how big your dataset is but it is inefficient to loop through @unpairedSidBinder inside the outer loop. You could split it into two stages, saving the matches in a hash of arrays - something like:
    my @sid = ('sid 1','sid 2','sid 7','sid 9'); my %hoa; foreach my $el (@unpairedSidBinder){ my ( $key, $data ) = split /\|/,$el; push( @{ $hoa{$key} }, $data ); } foreach my $key (@sid) { my $ary = $hoa{$key} or next; my $temp = join(" ", @$ary ); print "$key|$temp\n"; }

      Thank you

Re: concatenating elements of an array in twos
by Marshall (Canon) on Aug 17, 2016 at 00:06 UTC
    If the situation is that these things are sequential as in your example data, this is a common pattern that does not require the storage of the output array.
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $line = <DATA>; #get started with first line chomp $line; my ($prev_sidpart,$prev_rest) = split /\|/, $line; while (my $line = <DATA>) { chomp $line; my ($cur_sidpart,$cur_rest) = split /\|/, $line; if ($prev_sidpart eq $cur_sidpart) { $prev_rest .= " $cur_rest"; } else { print "$prev_sidpart|$prev_rest\n"; ($prev_sidpart,$prev_rest) = ($cur_sidpart,$cur_rest); } } print "$prev_sidpart|$prev_rest\n"; #last record =prints: sid 1|FLPSDFFPS LLWFHISCL WIRTPPAYR YVNVNMGLK sid 2|FLPSDFFPS LLWFHISCL FLPSDFFPS ELMNLATWV sid 7|FLPSDFFPS ATVELLSFL VWIRTPPAY LLSFLPSDF sid 9|VWIRTPPAY LLDTASALY FGRETVLEY PSDFFPSVR =cut __DATA__ sid 1|FLPSDFFPS LLWFHISCL sid 1|WIRTPPAYR YVNVNMGLK sid 2|FLPSDFFPS LLWFHISCL sid 2|FLPSDFFPS ELMNLATWV sid 7|FLPSDFFPS ATVELLSFL sid 7|VWIRTPPAY LLSFLPSDF sid 9|VWIRTPPAY LLDTASALY sid 9|FGRETVLEY PSDFFPSVR
Re: concatenating elements of an array in twos
by Anonymous Monk on Aug 16, 2016 at 23:18 UTC
    Your problem specification unfortunately is quite ambiguous, but maybe something like this?
    my %r; for(@unpairedSidBinder){ my($sid,$foo)=split(/\|/); push(@{$r{$sid}//=[]},$foo); } print(map("$_|".join(' ',@{$r{$_}})."\n",keys(%r)));

      Thank you, oh Great One! It worked

      Thank you, but I dont want to use a hash as I need to maintain the duplicate values of the peptides. I'll give it a go though!

Re: concatenating elements of an array in twos
by Anonymous Monk on Aug 16, 2016 at 23:33 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1169870 use strict; use warnings; $_ = <<END; sid 1|FLPSDFFPS LLWFHISCL sid 1|WIRTPPAYR YVNVNMGLK sid 2|FLPSDFFPS LLWFHISCL sid 2|FLPSDFFPS ELMNLATWV sid 7|FLPSDFFPS ATVELLSFL sid 7|VWIRTPPAY LLSFLPSDF sid 9|VWIRTPPAY LLDTASALY sid 9|FGRETVLEY PSDFFPSVR END s/^(sid\s+\d+\|).*\K\n\1/ /gm; print;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1169870]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found