[Buffalo-pm] Question: How to iterate through 2 arrays and merge columns

Jim Brandt cbrandt at buffalo.edu
Mon Jul 4 14:56:02 PDT 2005


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



More information about the Buffalo-pm mailing list