Sort multidimensional Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort multidimensional Array
# 1  
Old 03-07-2019
Sort multidimensional Array

Hello

I have a problem.
I create a Multidimensional Array Like this:
Code:
        ENTRY["HOST1"]="$kunnum-$host"
        ENTRY["HOST2"]="$host"
    ENTRY["COMMENT"]="# $3"
    for key in "${!ENTRY[@]}"; do
        ENTRIES[$2"5",$key]=${ENTRY[$key]} # INDEX=IP(5)
    done

Code:
declare -p
declare -A ENTRIES=([205,HOST1]="unas15533" [205,HOST2]="unas" [1025,COMMENT]="# RDP-Terminal 2" [775,COMMENT]="# WLAN Access Point" [1015,IP]="111.122.133.101" [1025,IP]="111.122.133.102" [775,IP]="111.122.133.77" [205,COMMENT]="# UNAS" [775,HOST1]="15533-wlan" [1015,HOST2]="rdp1" [1015,HOST1]="15533-rdp1" [775,HOST2]="wlan" [1015,COMMENT]="# RDP-Terminal 1" [1025,HOST2]="rdp2" [1025,HOST1]="15533-rdp2" [205,IP]="111.122.133.20" )

Now i have to sort this by the "INDEX"
Code:
([205,HOST1]="unas15533"

in this case it would be 205
I have to sort all elements and then i want to give them formated out.

How can I sort this array?

Thank you so much for your help.




Moderator's Comments:
Mod Comment Please use CODE tags consistently as required by forum rules!

Last edited by RudiC; 03-07-2019 at 03:45 AM.. Reason: Added CODE tags.
# 2  
Old 03-07-2019
Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, directory structures, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.
# 3  
Old 03-07-2019
Quote:
Originally Posted by Marti95
I have to sort all elements and then i want to give them formated out.

How can I sort this array?
In addition to what RudiC already said (and, by the way, if your programming language is not some shell - i assume that it is, but my assumption might be wrong - it would be nice to know which one that is). Notice, though, that most shells don't support multidimenisonal arrays and especially not assiociative arrays:

What exactly do you mean by "sort all the elements"? For a sorting you need some key - alphabetical, numerical, by date or whatever - and something to sort. An array, regardless of being multidimensional or not, contains several elements. The elements have an index - basically their position in the array - and some content. Do you want to sort the contents or the indices to be sorted?

If it is the indices: these are multi-value items in a multidimensional array and it is not clear which part of these take priority: i.e. if you have 3 entries, [1,10], [2,5] and [10,1], which one is supposed to be first? If the first element takes priority the order would be [1,10], [2,5], [10,1], if the second element takes priority you have [10,1], [2,5], [1,10], if the sum of both entries is the sort criteria it is [2,5], [1,10], [10,1], etc..

If it is the contents you are trying to sort you simply need to traverse through the indices of one dimension after the other using simple for-loops, i.e. for a 3-dimensional array with numerical indices (pseudocode):

Code:
for i in 1..10
     for j in 1..10
          for k in 1..10
               print array[i,j,k]
          next
     next
next

and sort the resulting list.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 03-07-2019
Code:
shuf -e ${!ENTRIES[@]} | sort -V | while read d; do echo $d=${ENTRIES[$d]}; done

--- Post updated at 12:01 ---

can play with the options of the "sort" command Smilie
This User Gave Thanks to nezabudka For This Post:
# 5  
Old 03-07-2019
Try also
Code:
echo "${!ENTRIES[*]}" | sort -n | while read IX; do printf "%-15s%-15s\n" $IX ${ENTRIES[$IX]}; done
205,COMMENT    # UNAS         
205,HOST1      unas15533      
205,HOST2      unas           
205,IP         111.122.133.20 
775,COMMENT    # WLAN Access Point
775,HOST1      15533-wlan     
775,HOST2      wlan           
775,IP         111.122.133.77 
1015,COMMENT   # RDP-Terminal 1
1015,HOST1     15533-rdp1     
1015,HOST2     rdp1           
1015,IP        111.122.133.101
1025,COMMENT   # RDP-Terminal 2
1025,HOST1     15533-rdp2     
1025,HOST2     rdp2           
1025,IP        111.122.133.102

This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-07-2019
Thank you for your answers

And sorry for the few informations.
I used bash
Code:
#!/bin/bash

I have to sort this nummeric.
After sorting I want to give this out in a File
In this order
Code:
# IP - HOST1  - HOST2 - COMMENT

my problem is to find an efficent way for that.
Because i dont know the index of the array entry
It could be something between 10 and 2550

Code:
ENTRIES[INDEX,$key]

I gona try your methods for sorting the array. Thanks
# 7  
Old 03-07-2019
Does reversing a sort key help?
Code:
$ IFS=$'\n'
$ echo "${!ENTRIES[*]}" | sort -t, -k1,1n -k2r | while read IX; do printf "%-15s%-15s\n" $IX ${ENTRIES[$IX]}; done
205,IP         111.122.133.20 
205,HOST2      unas           
205,HOST1      unas15533      
205,COMMENT    # UNAS         
775,IP         111.122.133.77 
775,HOST2      wlan           
775,HOST1      15533-wlan     
775,COMMENT    # WLAN Access Point
1015,IP        111.122.133.101
1015,HOST2     rdp1           
1015,HOST1     15533-rdp1     
1015,COMMENT   # RDP-Terminal 1
1025,IP        111.122.133.102
1025,HOST2     rdp2           
1025,HOST1     15533-rdp2     
 1025,COMMENT   # RDP-Terminal 2

Don't forget to reset your IFS afterwards.


EDIT: or, even better to get HOST1 and 2 in the right order:

Code:
echo "${!ENTRIES[*]}" | sort -t, -k1,1n -k2,2.2r -k2.3 | while read IX; do printf "%-15s%-15s\n" $IX ${ENTRIES[$IX]}; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multidimensional array

I am learning about bash system variables, such as $ , @ and #. I have this piece of script implementing an array and it is doing its job just fine. This is not the only array I will be using. Just for ease of maintenance and more coding I would like to have the arrays in two dimensional... (4 Replies)
Discussion started by: annacreek
4 Replies

2. Shell Programming and Scripting

How to deal with multidimensional array in awk?

Hi all! I would like to know how to print $0 when using multidimensional array like below time being I am using for loop to print columns like this awk 'FNR==1{i++} {for(k=1;k<=NF;k++)A=$k} END{for(j=1;j<=25;j++) print A,A,A,A,A,A,A,A,A,A,A,A,A,A}' file1 file2 so here my problem is I... (5 Replies)
Discussion started by: Akshay Hegde
5 Replies

3. Shell Programming and Scripting

gawk - How to loop through multidimensional array?

I have an awk script that I am writing and I needed to make use of a multidimensional array to hold some data... Which is all fine but I need to loop through that array now and I have no idea how to do that. for a regular array, the following works: ARRAY for(var in ARRAY) { ... } ... (5 Replies)
Discussion started by: trey85stang
5 Replies

4. Programming

Multidimensional arrays and sort.

My language is C++. I have a multidimensional vector that I would like to sort by a specific "cell" or "field" within the main vector. Does anyone have any information on how to do this? I have searched all over the internet and every reference manual I can find. So far I have found very little to... (2 Replies)
Discussion started by: sepoto
2 Replies

5. Programming

Multidimensional array of strings with vector.

I've been struggling with this for quite some time. I decided I should get some help with this. Nothing is working. I'm getting a segmentation fault or out of bounds error when I try to load the entries in the for loop.I'm really frustrated. :mad: Compiling isn't the problem. It's crapping out on... (5 Replies)
Discussion started by: sepoto
5 Replies

6. Shell Programming and Scripting

multidimensional array in awk

Hi, I was trying to process a file with the help of awk. I want to first display all the rows that contains 01 and at the end of processing I have to print some portion of all the lines. like below. Output expected: (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

7. Programming

multidimensional array using c++ vector

Hi! I need to make dynamic multidimensional arrays using the vector class. I found in this page How to dynamically create a two dimensional array? - Microsoft: Visual C++ FAQ - Tek-Tips the way to do it in 2D, and now i'm trying to expand it to 3D but i don't understand how is the operator working,... (0 Replies)
Discussion started by: carl.alv
0 Replies

8. Shell Programming and Scripting

AWK multidimensional array

In a single dim. awk array, we can use : <index> in <array name> to determine whether a particualar index exists in the array or not. Is there a way to achieve this in a awk multi dim. array ? (4 Replies)
Discussion started by: sinpeak
4 Replies

9. Shell Programming and Scripting

Awk multidimensional Array

Hello Experts,, Can anybody give me a brief idea what is following bold letter statement is for!! what is the term called so that I can google for it.. It seems to be an array inside another array.. awk' /TXADDR/ { txaddr=$NF } ##understood /TXDATA/ { txdata]=$NF... (1 Reply)
Discussion started by: user_prady
1 Replies

10. Shell Programming and Scripting

multidimensional array in perl

i'm trying to open a file with three or more columns and an undetermined, but finite number of rows. I want to define an array for each row with each element of the row as a sub array. The columns are separated by tabs or spaces. Here's the file: 12x3.12z34b.342sd3.sds 454.23.23.232 ... (9 Replies)
Discussion started by: prkfriryce
9 Replies
Login or Register to Ask a Question