Multidimensional array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multidimensional array
# 1  
Old 08-17-2018
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 fashion so I can put all of them in one physical place.



In pseudocode

Code:
array 

         arraySYSTEM=( "$@" )
         arrayUSB=( "$@" )
         arrayXXX=( "$@" )

I am still not to sure how the symbols $@ used in this code actually work.

So - how do I implement two dimension array using system arables?





Code:
 echo "#declare an arraySYSTEM   @line $LINENO "  
 788  arraySYSTEM=( "$@" )
 789 #temporary use lsusb commnds  
 790 set -- lsusb " Debug component SYSTEM " "TEST parameter lsusb -v" "lsusb -$
 791 echo "$#"       # number of elements in array
 792 echo "$@"       # contents  of array 
 793 # do common function  
 794                 ;;

# 2  
Old 08-17-2018
With no indication of what you want the second array dimension to be nor what values you want to use as indices into your array, I have no idea how to give you any guidance on this subject.

I don't find your pseudocode helpful in understanding what you're trying to do. I don't see anything indicating that there is a two dimensional array in that pseudocode and I have no idea why you would need three arrays that all contain the same number of elements and have each corresponding element of those three different arrays contain identical strings.

In line 790 of the code you showed us, there are mismatched double-quotes. That is likely to cause a syntax error somewhere in one of the following lines unless you manage to match the last quote with another mismatched double-quote on a line somewhere following line 794 in code that you haven't bothered to show us. If you have read the manual page, and there is something about these three variables that you don't understand, ask us specific questions about what you don't understand. If you just say you don't understand what $$ does, about all we can do is quote what you will find on the bash manual page.

Depending on what you're trying to do, you might also need to use an associative array instead of an indexed array. As far as I know, bash doesn't provide multi-dimensional arrays, but they can sometimes be simulated using associative arrays. Associative arrays were added to ksh in 1993. I don't know what version of bash you need in order to be able to use associative arrays.

Since you haven't bothered to tell us what operating system you're using and what version of bash you're using, we can't make many useful suggestions.

How $$, $@, "$@", and $# work are all explained very clearly on the bash man page on your system and in the bash manual pages available under the "Man" tab at the top of The UNIX & Linux Forums home page.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-18-2018
bash --version
GNU bash, version 4.4.12(1)-release (arm-unknown-linux-gnueabihf)


What I am trying to do is to pass variables / arguments to a common function.
At present I am using build-in "position (?) variables" "$" and need more that 10 at a time.

I was hoping multidimensional array / indexing would help , apparently not a bash

"standard "option. ( I found another source addressing the issue I'll try to use.)



It is not necessary to show your superiority attitude to beginner likes me, especially your RTFM comment was not appreciated.

Moderator's Comments:
Mod Comment Neo: annacreek has been issued a light infraction and written warning for the comment above. Further comments like this will result in a ban from this site.


Perhaps you have never made a typo in you life too.
# 4  
Old 08-19-2018
Quote:
Originally Posted by annacreek
It is not necessary to show your superiority attitude to beginner likes me, especially your RTFM comment was not appreciated.
I reviewed Don Cragun's comments and he was correct asking "annacreek" politely if he or she had reviewed the docs.

I did not see any "superior attitude" or anything negative in Don's reply, and this is a warning to annacreek.

If you insult anyone here again, especially moderators who are trying to help you, you will be banned.

Do not come here and insult people who are helping you.
This User Gave Thanks to Neo For This Post:
# 5  
Old 08-19-2018
Hello!

In case you forgot to read the forum rules, here is quick copy.

Quote:
RULES OF THE UNIX AND LINUX FORUMS


(1) No flames, shouting (all caps), sarcasm, bullying, profanity or arrogant posts.

(2) No negative comments about others or impolite remarks. Be patient.

(3) Refrain from idle chatter that does not contribute to the knowledge base. This does not apply to the forums in The Unix Lounge which are for off-topic discussions.

(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post or send a private message where your goal is to get an answer more quickly.

(5) Search the forums database with your keywords before asking.

(6) Do not post classroom or homework problems.

(7) No job postings from headhunters or recruiters except in The Unix Forums Job Board. See How to Post to The UNIX Forums Job Board for information on using the Job Board.

(8) No BSD vs. Linux vs. Windows or similar threads.

(9) Edit your posts if you see spelling or grammar errors (don't write in cyberchat or cyberpunk style). English only.

(10) Don't post your email address and ask for an email reply. Don't send a private message with a technical question. The forums are for the benefit of all, so all Q&A should take place in the forums.

(11) Post questions with descriptive subjects. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

(12) These are not hacker boards so hacker related posts will be promptly deleted or moderated.

(13) The forum administrators reserve the right to prune, move or edit posts that do not adhere to the rules or are technically inaccurate.

(14) The forum administrators reserve the right to remove users or change their posting status to read only without notice if any rules are not followed.

(15) No smoking in the forums.
Cheers.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort multidimensional Array

Hello I have a problem. I create a Multidimensional Array Like this: ENTRY="$kunnum-$host" ENTRY="$host" ENTRY="# $3" for key in "${!ENTRY}"; do ENTRIES=${ENTRY} # INDEX=IP(5) donedeclare -p declare -A ENTRIES=(="unas15533" ="unas" ="# RDP-Terminal 2"... (12 Replies)
Discussion started by: Marti95
12 Replies

2. Shell Programming and Scripting

Multidimensional array:awk error

awk -F'\t' -v OFS='\t' ' { if($2 in arr) { #print "Sahi", NR,arr for(k=2;k<=NF;k++){ # sum]+=$2 } } else { arr=NR #print "awk",NR for (k=3;k<=NF ; k++){ sum=$k } } } (7 Replies)
Discussion started by: genome
7 Replies

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

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

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