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

Shankar, Ganesh Ganesh.Shankar at RoswellPark.org
Tue Jul 5 06:17:23 PDT 2005


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.


More information about the Buffalo-pm mailing list