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

Shankar, Ganesh Ganesh.Shankar at RoswellPark.org
Thu Jul 7 02:48:59 PDT 2005


Ok, after my previous post, I was able to resolve the '@red_elements is not imported' error by placing the 'our @red_elements;' at the top of the program.

The 'uninitialized value' warning is still present, but the script runs.  Code snippet is at the end of the message.

However, the format of the output is:

red1 tab green1 tab red2 tab green2 tab etc.

What I need is :

red1 tab red2 tab red3 tab red4 tab green1 tab green2 tab green3 tab green4.

I thought that since the strings were in the correct order, 

print OUTFILE  $red_elements[$i],$green_elements[$i]; 

would just concatanate the two strings. Not!

I tried 

print OUTFILE  $red_elements[$i]."\t".$green_elements[$i];

but that results in the same pattern, but with an extra tab in between elements.


Thanks for any help.

-Ganesh
<code>

open( REDOPEN, $red_file ) or die $!;
	print "opened " . $red_file . "\n";
	my $i;
	for ( $i = 0 ; $i < 21 ; $i++ ) {
		print "looping through header line $i \n";
		<REDOPEN>;
	}
	my @red = <REDOPEN>;
	foreach my $red_line (@red) {
		our @red_items = split( '\t', $red_line );
		push( @red_elements,		
				$red_items[6]. "\t", $red_items[7]."\t",
				$red_items[8]. "\t", $red_items[9]. "\t"
		);
	}    #end foreach my $red_line


	for (my $i= 0;$i <= $#red_elements; $i++){
	print OUTFILE  $red_elements[$i]."\t".$green_elements[$i];
			} 
</code>


-----Original Message-----
From: Jim Brandt [mailto:cbrandt at buffalo.edu]
Sent: Wed 7/6/2005 8:11 AM
To: Shankar, Ganesh
Cc: buffalo-pm at pm.org
Subject: Re: [Buffalo-pm] Question: How to iterate through 2 arrays andmerge columns
 
I think my example mixed a few variable names as well, and that may  
have confused things.

Looking at your original code, you are doing:

> push(our @red_elements,
> ($red_items[6], "\t", $red_items[7], "\t",
> $red_items[8], "\t", $red_items[9], "\n"
> )
> );

The parentheses (which I didn't see before) will create an anonymous  
array in each element of @red_elements. This can work for you since  
you've created a 2-dimensional array, which is really a table. So I  
think if you modify my code to do this:

for (my $j=0;$j<=$#red_elements;$j++){
   # Iterate through outer loop, one row at a time.
   for ( my $i=0; $i <= 7; $i++ ){
     # You have eight columns, so this will go through each column  
and print it.
     print OUTFILE $red_elements[$j][$i];
   }

   # Repeat for green_elements.
   for ( my $i=0; $i <= 7; $i++ ){
     # You have eight columns, so this will go through each column  
and print it.
     print OUTFILE $green_elements[$j][$i];
   }

} # End outer loop.

Note that you'll need to modify your code building the red_elements  
since you are currently putting a new line as the last element and I  
think you want a tab.

That should work, but is a bit convoluted. I would simplify your  
earlier step to make it just a string rather than 8 elements in an  
array. For example:

push(our @red_elements, $red_items[6] . "\t" . $red_items[7] . "\t" .  
$red_items[8] . "\t" . $red_items[9] . "\t" );

The dot is the string concatenation operator, so it will push all of  
that together into a single string. I think if you do it this way, my  
first example will work.

Basically, it's playing with data structures two different ways.

For what it's worth, I think Kevin's suggestion was the quickest and  
cleanest, but you don't get to play with data structures in perl. :)

Good luck.

Jim

On Jul 5, 2005, at 10:33 PM, Shankar, Ganesh wrote:

> Your suggestions were on the right track but maybe I wasn't clear  
> as to what I wanted.  I need 4 cols from red followed by 4 cols  
> from green.  The following code gives me 4 pairs of red(no space) 
> green(tab)(tab).
>
> I tried:
> <code>
>     for (my $j=0;$j<=$#red_items;$j++){
>     push(@intermediate, $red_items[$j],$green_items[$j]);
>     }
> print OUTFILE @intermediate;
> </code>
>
> What results is:
> $red_items[6](no space)$green_items[6](2 tabs)$red_items[7](no  
> space)$green_items[7]
> (2 tabs)$red_items[8](no space)$green_items[8](2tabs)$red_items[9] 
> (nospace)$green_items[9]

==========================================
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