[Buffalo-pm] Question: How to iterate through 2 arraysandmerge columns

Kevin Eye eye at buffalo.edu
Tue Jul 5 07:34:47 PDT 2005


I don't understand why your suggestion wouldn't work, Dan. I don't see 
anything wrong with the pipes.

However, all these solutions seem quite inefficient. I think it's 
simplest to just open both input files at once, read a line from each, 
and immediately write the new line out like this:

open IN1, 'input_1.tab';
open IN2, 'input_2.tab';
open OUT, '>output.tab';

while(1) {
     $in_1_line = <IN1>;
     $in_2_line = <IN2>;
     last unless defined $in_1_line and defined $in_2_line; # quit when 
out of input
     chomp $in_1_line;
     chomp $in_2_line;
     @in_1_fields = split "\t", $in_1_line;
     @in_2_fields = split "\t", $in_2_line;
     print OUT join("\t", @in_1_fields[0..3], @in_2_fields[0..3])."\n";
     }

close IN1;
close IN2;
close OUT;

If you want fields other than 1, 2, 3, and 4 of each input file in the 
output, just change the array indices on the print line. If you always 
want all the fields, and every line always has the same number of 
fields, you could even get rid of the splitting and joining and just 
concatenate $in_1_line and $in_2_line to make it run faster.

  - Kevin

On Jul 5, 2005, at 10:08 AM, DANIEL MAGNUSZEWSKI wrote:

> Actually, after I sent this in, I realized that the pipe in the 
> external
> command would not work. Does anyone know how to properly connect the
> pipes?
>
>>>> "DANIEL MAGNUSZEWSKI" <dmagnuszewski at mandtbank.com> 07/05/05 10:02
> AM >>>
> For what you wanted, it looks like $red_items[$i] would be the four
> red
> elements.
>
> If I were to go about doing what you're trying to accomplish, I would:
>
> <CODE>
> # Not Tested
> my @red_elements     = `/bin/cat file1 | awk \'{print "$4 $5 $6
> $7"}\'`;
> my @green_elements = `/bin/cat file2 | awk \'{print "$4 $5 $6 $7"}\'`;
>
> open OUTFILE, ">outputfile" or die "Can't open file\n";
>
> for ( my $i=0; $i <= $#red_elements; $i++ ){
>    print OUTFILE $red_elements[$i], $green_elements[$i];
> }
>
> </CODE>
>
> Hope this helps.
>
> Daniel Magnuszewski
> CCNA
> M & T Bank
> dmagnuszewski at mandtbank.com
>
>>>> "Shankar, Ganesh" <Ganesh.Shankar at RoswellPark.org> 07/05/05 9:17
> AM
>>>>
> Thanks for your reply.
>
> The two arrays each hold 4 elements per line.  There are approx.
> 18,000
> lines per file. The final line should contain 8 elements, after
> processing.  The first 4 would be from @red_elements and the second 4
> would be from @green_elements.
>
> So, in your solution, would $red_items[$i] represent the entire line
> consisting of all 4 elements?
>
> -Ganesh
>
> -----Original Message-----
> From: Jim Brandt [mailto:cbrandt at buffalo.edu]
> Sent: Monday, July 04, 2005 5:56 PM
> To: Shankar, Ganesh
> Cc: buffalo-pm at pm.org
> Subject: Re: Question: How to iterate through 2 arrays and merge
> columns
>
> If the arrays are truly identical, you can iterate through either one
>
> using the C-style for loop and grab one item at a time from each. It
> would look something like (untested):
>
> for ( my $i=0; $i <= $#red_items; $i++ ){
>    print OUTFILE $red_items[$i], $green_items[$i];
> }
>
> This style for loop starts at the initial value of $i, iterates as
> long as the middle condition is true, and increments based on the
> last expression. In this case, it will iterate one number at a time.
>
> The $#red_items variable holds the last index of the array
> @red_items, so it will iterate to the last element of the array.
>
>
> On Jul 4, 2005, at 3:33 PM, Shankar, Ganesh wrote:
>
>>
>> Hello All,
>>
>> I'm learning perl to process microarray data and mostly I've found
> it
>> very useful.  However, I've come to a problem that I haven't been
> able
>> to solve.
>>
>> Background:  One experiment generates two raw (tab-delimited)data
>> files.
>>  The order of the lines in both these files are identical.  I need
> to
>> grab 4 columns from each file and horizontally merge them in the
>> resulting results file.
>>
>> I've opened,read,split and pushed the elements into two arrays in
>> their
>> own foreach loops.
>>
>> push(our @red_elements,
>> ($red_items[6], "\t", $red_items[7], "\t",
>> $red_items[8], "\t", $red_items[9], "\n"
>> )
>> );
>> }    #end foreach my $red_line
>>
>> push(our @green_elements,
>> ($green_items[6], "\t", $green_items[7], "\t",
>> green_items[8], "\t", $green_items[9], "\n"
>> )
>> );
>> }    #end foreach my $red_line
>>
>> What I need in the final file is :
>>
>> ($red_items[6], "\t", $red_items[7], "\t",
>> $red_items[8], "\t", $red_items[9], "\t",
>> $green_items[6], "\t", $green_items[7], "\t",
>> green_items[8], "\t", $green_items[9], "\n"
>>
>> How do I iterate through two arrays at once?  I've tried the
>> Array::Each
>> module, but I couldn't extend the example in the synopsis to
> multiple
>> columns.
>>
>> Thanks for any help.
>>
>> -Ganesh
>>
>> <ATT256146.txt>
>
> ==========================================
> Jim Brandt
> Administrative Computing Services
> University at Buffalo
>
>
>
> This email message may contain legally privileged and/or confidential
> information.  If you are not the intended recipient(s), or the
> employee
> or agent responsible for the delivery of this message to the intended
> recipient(s), you are hereby notified that any disclosure, copying,
> distribution, or use of this email message is prohibited.  If you have
> received this message in error, please notify the sender immediately
> by
> e-mail and delete this email message from your computer. Thank you.
> _______________________________________________
> Buffalo-pm mailing list
> Buffalo-pm at pm.org
> http://mail.pm.org/mailman/listinfo/buffalo-pm
>
>
> _______________________________________________
> Buffalo-pm mailing list
> Buffalo-pm at pm.org
> http://mail.pm.org/mailman/listinfo/buffalo-pm
>
>
> _______________________________________________
> Buffalo-pm mailing list
> Buffalo-pm at pm.org
> http://mail.pm.org/mailman/listinfo/buffalo-pm
>


--
Kevin Eye
Web Applications Developer
Creative Services and Marketing
University at Buffalo
330 Crofts Hall
Buffalo, NY 14260
eye at buffalo.edu
phone (716) 645-5000 x1435
fax (716) 645-3765



More information about the Buffalo-pm mailing list