Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays of arrays

Status
Not open for further replies.

Mattwatters

Technical User
Dec 2, 2003
7
0
0
GB
Hello

I am trying to split lines do the odd calculation and put the results into an array. basically an array of arrays. After this is done i am trying to sort this array on the 6th field of the arrays it holds. After this i am trying to print out the array to a html table but somewhere along the line somethings gone wrong cause my table is empty apart from the odd "15" in some columns - possibly to do with the number of array items. Could someone please let me know if there are any fundamenal flaws or if i am passing things properly. I know the data going into the code below is valid

$increment=0;
if($comp2 eq $comp1)
{
$line1 = $table1a{$comp2};
$line2 = $table2a{$comp2};
$line3= $table3a{$comp2} @fields1=split(/\s+/,$line1); @fields2=split(/\s+/,$line2);
@fields3=split(/\s+/,$line3);
$inorder[0]=$fields1[0]; $inorder[1]=$($fields1[1]); $inorder[2]=$fields1[3]+$fields2[3]+$fields3[3]/3; $inorder[3]=$fields1[4]+$fields2[4]+$fields3[4]/3; $inorder[4]=$fields1[5]+$fields2[5]+$fields3[5]/3; $inorder[5]=$inorder[2]+$inorder[3]+$inorder[4]/3;
$inorder[6]=$fields1[3];
$inorder[7]=$fields2[3];
$inorder[8]=$fields3[3];
$inorder[9]=$fields1[4];
$inorder[10]=$fields2[4];
$inorder[11]=$fields3[4];
$inorder[12]=$fields1[5];
$inorder[13]=$fields2[5];
$inorder[14]=$fields3[5];
$sorted[$increment] = @inorder;
$increment++;
}
@sorted1 =( sort { $a->[5] <=> $b->[5] } @sorted);

for $val1(@sorted1)
{
@sorted3=$val1;

Rest of Print code here - use @sorted3 to print

}

cheers
 
your problem is in this statement:
$sorted[$increment] = @inorder;
this will put the number of elements in @inorder in $sorted[increment] which is exactly what you are getting.
What are you trying to accomplish do there?
 
oh i see, you can't make an array of arrays, you can make an array of references:
$sorted[increment] = \@inorder;
 

Cheers for the reply - what i am trying to do is put the contents of @inorder into $sorted[$increment]

Isthis the wrong way to do this.

The increment statement is fine - the code before what i have shown is a for loop.

matt
 
You cannot store arrays inside of arrays. You can only store simple scalars. To simulate multidimensional arrays (array of arrays) you have to use references as mactonio suggested.

$sorted[$increment] = \@inorder;

You now have a reference to the contents of @inorder stored in $sorted[$increment]. It does not actually contain the contents of @inorder, it only tells where the contents of @inorder are stored at. Then, by using dereferencing, you can get the contents of @inorder.

Currently, if you print out the contents of $sorted[$increment] you will see something like this:

ARRAY0x023465. It is the pointer that tells where @inorder is stored. To dereference $sorted[$increment], you have to do something like this:

@inorder = qw(1 2 3 4 5);
$sorted[$increment] = \@inorder;

print $sorted[$increment][0]; #prints 1;
print $sorted[$increment][3]; #prints 4;
print &quot;\n&quot;;
print &quot;$_\n&quot; foreach (@{$sorted[$increment]}) ; #prints 1 2 3 4 5
 
cheers for that racklet. just one slight hitch is that the value of @$val1 is always the same at the end - i was expecting muliple sorted values



..
..

$inorder[13]=$fields2[5];
$inorder[14]=$fields3[5];
$sorted[$increment] = \@inorder;
$increment++;
}
@sorted1 =( sort { $a->[5] <=> $b->[5] } @sorted);

for $val1(@sorted1)
{
print @$val1[5];

cheers
 
Can you post some sample data, so I can see how it fits into the context of the program? Also, an idea of what you are expecting the output to be versus what it actually is would be nice.
 
i am expecting at the top of my code i posted $comp2 to equal $comp1 three times. Every time in matches it will pull three lines of text through from three hashes. These are split and put into an array with 15 elements in. The three times this should happen the array that is created with 15 elements is then stuck into an array itself so by the end of the loop there should be an array called sorted which has three arrays of 15 elements each. I then want to sort this array on the 6th element of the arrays it hold.

This 15 element array has the following characteristics

Element 0 - surname
Element 1 - forename
Element 2-13 - integers.

I am printing out the array but sorted on the 6th element but i keep getting the same line three times instead of three different ones i.e. same surname three times instead of the three different surnames.

thankyou
 
Have you verified that the values of comp2 and comp1 are changing each time through the loop? If not, then you would get redundant results. Also, how are you printing out the values of your arrays? If you do not increment through the array, but stick on the same index, then you will also get repeat values.

Here is what your array should look like:

@sorted = ([1..15 of a], #index 0
[1..15 of b], #index 1
[1..15 of c]); #index 2

To get the sixth value of each array you have to do something like this:

print &quot;@{$_}[6]\n&quot; foreach (@sorted);

To see if your array actually holds the values that you want, you should print out the entire thing, like so:

print &quot;@{$_}\n&quot; foreach (@sorted);

Also, for more reading on the subject, you should see an excellent post on Devshed, here:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top