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

Shankar, Ganesh Ganesh.Shankar at RoswellPark.org
Thu Jul 7 01:37:42 PDT 2005


Hello All,

Again,thanks for taking the time to walk me through this.

As you suggested, I started working with the data as strings.  This made it conceptually easier to deal with and I wanted to avoid thinking about anonymous arrays at this point.

The approach at this point looks like this:-


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(our @red_elements,		
				$red_items[6]. "\t", $red_items[7]."\t",
				$red_items[8]. "\t", $red_items[9]. "\t"
		);
	}    #end foreach my $red_line

...There is a similar block for dealing with green data...
Here is the loop to concatanate the two strings together.

for (my $i= 0;$i <= $#red_elements; $i++){
			print OUTFILE  $red_elements[$i],$green_elements[$i];
			}


When I execute this, I get the following error:-

Variable "@red_elements" is not imported at line 80 (#1)
    (F) While "use strict" in effect, you referred to a global variable that
    you apparently thought was imported from another module, because
    something else of the same name (usually a subroutine) is exported by
    that module.  It usually means you put the wrong funny character on the
    front of your variable.

Line 80 corresponds to this line: for (my $i= 0;$i <= $#red_elements; $i++){

I thought declaring @red_elements to be 'our' made it available throughout the program?  Is scoping the problem?

I thought I'd cheat, and turned off 'use strict'.

At that point, I got an uninitialized variable error pointing to this line:

push(our @red_elements,

Looks like the two errors are related and solving the first error should take care of the uninitialized variable error.

Any pointers?  Where can I read about this?

Thanks again.

-Ganesh Shankar



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