CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Storing an array in another array

 



kwatts59
Novice

Dec 11, 2014, 12:19 PM

Post #1 of 4 (1100 views)
Storing an array in another array Can't Post

I am trying to store the array "@row" into another array "@genes". Below is the code:


Code
$gene_query = "select locus_id,chromosome,start_position,end_position from $table_name where feature = \"gene\" order by chromosome,start_position"; 
$sqlquery1 = $dbh->prepare($gene_query) or die "Cannot prepare query \n";
$rv1 = $sqlquery1->execute or die "Cannot execute query \n";
@row = $sqlquery1->fetchrow_array();

@genes = ();
$gene_count = 0;
while (defined $row[0])
{
$gene_count++;
@genes[$gene_count] = @row;
print "$gene_count\t$genes[$gene_count][0]\t$genes[$gene_count][1]\n"; @row = $sqlquery1->fetchrow_array();
}
$sqlquery1->finish;


The program just prints out the gene_count but not anything in the genes array.
Any help would be appreciated.
Thanks in advance.


FishMonger
Veteran / Moderator

Dec 11, 2014, 12:26 PM

Post #2 of 4 (1097 views)
Re: [kwatts59] Storing an array in another array [In reply to] Can't Post

There are a number of improvements I'd make, but the fix to your problem is this:

change:

Code
print "$gene_count\t$genes[$gene_count][0]\t$genes[$gene_count][1]\n";


to:

Code
print "$gene_count\t$row[0]\t$row[1]\n";


Why are you selecting 4 fields, but only output the first 2?


(This post was edited by FishMonger on Dec 11, 2014, 12:27 PM)


kwatts59
Novice

Dec 11, 2014, 2:16 PM

Post #3 of 4 (1087 views)
Re: [FishMonger] Storing an array in another array [In reply to] Can't Post

Thank you for your timely response.
What I have presented in my previous post was a simplified version of the program I am trying to write.

I want to pull selected records from a database table and save the records into the @genes array. I will use the @genes array later in the program for analysis.

The print command was just there to make sure the @genes array was storing the data. I will remove the print command once I can be certain the data is being stored correctly.

What should be printing is something like

Code
1     gene12        chr01 
2 gene18 chr03
3 gene35 chr07
4 gene51 chr10
etc...


but all that is printing is

Code
1 
2
3
4
etc...


This demonstrates the the @genes array is not being loaded properly.


FishMonger
Veteran / Moderator

Dec 11, 2014, 2:33 PM

Post #4 of 4 (1080 views)
Re: [kwatts59] Storing an array in another array [In reply to] Can't Post

You need to add a reference to @row to the @genes array. The easiest and best way to do that is via the push function.


Code
push @genes, \@row;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives