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!

Sorting an array of arrays, with Latex Output...

Status
Not open for further replies.

DunLudjin

Technical User
Jun 17, 2009
11
US
I have an array that I'm trying sort.

The array is

@parts = (1,2602344000,Ball Valve,1.0000,array(memaddress),2,2602345000,Gate Valve,2.0000,array(memaddress)...)

The array(memaddress) contains tags (V1) for the first and (V23,V40) for the second.

The record is really
1,2602344000,Gate Valve,1.0000,array(memaddress)
2,2602345000,Ball Valve,2.0000,array(memaddress)


I need to sort the records like an produce Latex output similar to this:

\section{Valve} (Taken from the Valve in the third slot)

\subsection{Ball Valve} (Taken From the third slot)
\begin{table}
2602345000 & V1 \\ (Sorted by the Second Slot,then array)
\end{table}

\subsection{Gate Valve}
\begin{table}
2602344000 & V23 \\
& V40 \\
\end{table}


Should I try to convert this array to a hash of arrays for the sorting? Also, any suggestions on the printout of the Latex output?


 
Can you change the way those records are loaded into @parts, or is that part already set in stone? If you can change that part, where does the data come from originally, and in what format?

Annihilannic.
 
Yes, either change the structure of @parts as it's being created, or else restructure it so it can be sorted easier. Here's an example (I've just used strings instead of the arrayrefs to illustrate):
Code:
my @parts = (1,2602344000,'Ball Valve',1.0000,'array(memaddress)',2,2602345000,'Gate Valve',2.0000,'array(memaddress)');

my @to_sort;

my $i = 0;
while( $i < @parts ) {
   push( @to_sort, [ @parts[ $i .. $i + 4 ] ] );
   $i += 5;
}

I don't quite understand what ordering you're looking for when you sort.
 
I don't understand right what you want to do.
For example you have 2 records in your array
Code:
(1 2602344000 Ball Valve 1 array(memaddress))
(2 2602345000 Gate Valve 2 array(memaddress))
if you use what ishnid suggested and want to sort the array by second slot in descending order, i.e.
Code:
@sorted = sort {@$b[1]<=>@$a[1]} @to_sort;
then you get this
Code:
(2 2602345000 Gate Valve 2 array(memaddress))
(1 2602344000 Ball Valve 1 array(memaddress))
Then IMHO your output should be
Code:
[COLOR=red]\subsection{Gate Valve}[/color]
\begin{table}
2602345000 & V1 \\ 
\end{table}

[COLOR=red]\subsection{Ball Valve}[/color]
\begin{table}
2602344000 & V23 \\
 & V40 \\
\end{table}
But in your example above you mix the data from several records in your LaTeX subsections:
In the 1.subsection you take the third element from record 1 and second element of record 2 (or 1.element of sorted array of all second elements from all records).
If you want to do so, you can extract second elements of every record in an independent array, sort it and then compose your output.
 
You are correct. I mixed the gate and ball valves. I think I see how it is done, using the second array. I'll give it a shot and see how that works.

Thanks!
 
When it is so, than this could be, what you probably searching for:
Code:
[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#804040][b]use warnings[/b][/color];

[COLOR=#0000ff]# data structure[/color]
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@array1[/color] = ([COLOR=#ff00ff]'[/color][COLOR=#ff00ff]V1[/color][COLOR=#ff00ff]'[/color]);
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@array2[/color] = ([COLOR=#ff00ff]'[/color][COLOR=#ff00ff]V23[/color][COLOR=#ff00ff]'[/color], [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]V40[/color][COLOR=#ff00ff]'[/color]);
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@parts[/color] = ([COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]2602344000[/color],[COLOR=#ff00ff]'[/color][COLOR=#ff00ff]Ball Valve[/color][COLOR=#ff00ff]'[/color],[COLOR=#ff00ff]1.0000[/color],[COLOR=#008080]\@array1[/color],
             [COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]2602345000[/color],[COLOR=#ff00ff]'[/color][COLOR=#ff00ff]Gate Valve[/color][COLOR=#ff00ff]'[/color],[COLOR=#ff00ff]2.0000[/color],[COLOR=#008080]\@array2[/color]);

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@to_sort[/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$i[/color] = [COLOR=#ff00ff]0[/color];
[COLOR=#804040][b]while[/b][/color]( [COLOR=#008080]$i[/color] < [COLOR=#008080]@parts[/color] ) {
   [COLOR=#804040][b]push[/b][/color]( [COLOR=#008080]@to_sort[/color], [ [COLOR=#008080]@parts[/color][ [COLOR=#008080]$i[/color] .. [COLOR=#008080]$i[/color] + [COLOR=#ff00ff]4[/color] ] ] );
   [COLOR=#008080]$i[/color] += [COLOR=#ff00ff]5[/color];
}

[COLOR=#0000ff]# sort the records[/color]
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@sorted[/color] = [COLOR=#804040][b]sort[/b][/color] {[COLOR=#008080]@$b[/color][[COLOR=#ff00ff]1[/color]]<=>[COLOR=#008080]@$a[/color][[COLOR=#ff00ff]1[/color]]} [COLOR=#008080]@to_sort[/color];


[COLOR=#0000ff]# printing output[/color]
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\\[/color][COLOR=#ff00ff]section{Valve}[/color][COLOR=#6a5acd]\n\n[/color][COLOR=#ff00ff]"[/color];
[COLOR=#804040][b]foreach[/b][/color] [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$record[/color] ([COLOR=#008080]@sorted[/color]) {
 [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$slot2[/color] = [COLOR=#008080]@$record[/color][[COLOR=#ff00ff]1[/color]];
 [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$slot3[/color] = [COLOR=#008080]@$record[/color][[COLOR=#ff00ff]2[/color]];
 [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@array[/color] = @{[COLOR=#008080]@$record[/color][[COLOR=#ff00ff]4[/color]]}; [COLOR=#0000ff]# dereference the array[/color]
 [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$array_string[/color] = [COLOR=#804040][b]join[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#ff00ff] [/color][COLOR=#6a5acd]\\\\\n[/color][COLOR=#ff00ff] & [/color][COLOR=#ff00ff]"[/color], [COLOR=#008080]@array[/color]); [COLOR=#0000ff]# join array elements[/color]
 [COLOR=#0000ff]# print LaTeX subsection[/color]
 [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\\[/color][COLOR=#ff00ff]subsection{[/color][COLOR=#008080]$slot3[/color][COLOR=#ff00ff]}[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color].
       [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\\[/color][COLOR=#ff00ff]begin{table}[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color].
       [COLOR=#ff00ff]"[/color][COLOR=#008080]$slot2[/color][COLOR=#ff00ff] & [/color][COLOR=#008080]$array_string[/color][COLOR=#ff00ff] [/color][COLOR=#6a5acd]\\\\\n[/color][COLOR=#ff00ff]"[/color].  
       [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\\[/color][COLOR=#ff00ff]end{table}[/color][COLOR=#6a5acd]\n\n[/color][COLOR=#ff00ff]"[/color];
}
Output:
Code:
\section{Valve}

\subsection{Gate Valve}
\begin{table}
2602345000 & V23 \\
 & V40 \\
\end{table}

\subsection{Ball Valve}
\begin{table}
2602344000 & V1 \\
\end{table}
 
Thank you all very much for your help! I don't know what I would accomplish without such help.

Shawn Way
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top