Shell quiz: emulate an associative array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell quiz: emulate an associative array
# 1  
Old 05-20-2009
Shell quiz: emulate an associative array

Most shells flavors do not have associative arrays a.k.a. maps.
  • How would you emulate an associative array?
I had this problem once and found a working solution, but I don't want to spoil the game hence I wont tell it.
Wonder if anyone comes up with something better.
# 2  
Old 05-20-2009

Code:
validname()
 case $1 in
   [!a-zA-Z_]* | *[!a-zA-Z0-9_]* ) return 1;;
 esac

setvar()
{
  validname "$1" || return 1
  eval "$1=\$2"
}

getvar()
{
  validname "$1" || return 1
  eval "printf '%s\n' \"\${$1}\""
}


## sample run
n=whatever
setvar board_$n Testing || { echo bad; exit 1; }
getvar board_$n


Last edited by cfajohnson; 05-21-2009 at 02:13 PM..
# 3  
Old 05-21-2009
@cfajohnson

Good!

That solution leverages the shell own symbol table (forget my inaccuracy), therefore it is efficient.

The array name board is implemented as a prefix of the many variable names this technique generates.
Of course to avoid name clash you have to remember to not use that prefix in the name of other variables that your program may have.

If I can say anything, I would like to point out that a better approach would be to have setvar and getvar use an internal fixed prefix. The fixed prefix would be something like aa4732_, therefore the chances of it to be used in other parts of the program would be very small.

Perhaps the call syntax can be more clear and useful this way:
Code:
setvar arrayname key value
getvar arrayname key

Code:
setvar colors "yellow" "#FFFF00" # would internally do aa4732_colors_yellow="#FFFF00"
setvar colors "red" "#FF0000"
getvar colors "yellow"
getvar colors "red"

A serious issue is that keys can only be strings in the alphabet [A-Za-z0-9_].
Do anyone see an approach that doesn't have this issue and that is reasonably quick for a small key set?

Last edited by colemar; 05-21-2009 at 06:30 AM.. Reason: Avoid: setvar colors "yellow" == setvar color "syellow"
# 4  
Old 05-21-2009
while i see its quite interesting, but what's the point of these?
# 5  
Old 05-21-2009
Quote:
Originally Posted by ghostdog74
... but what's the point of these?
What's the point of... what?

The point of implementing an associative array is to have a useful data structure that is missing in just about any shell except ksh93.
# 6  
Old 05-21-2009
Quote:
Originally Posted by colemar
What's the point of... what?

The point of implementing an associative array is to have a useful data structure that is missing in just about any shell except ksh93.
... , zsh and bash4.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loading associative array from exported function

Hello. I have an export of an associative array build using declare -p SOME_ARRAY_NAME > SOME_FILE_NAME.txt. Producing some thing like declare -A SOME_ARRAY_NAME=( ="some_text" ="a_text" ......... ="another_text" ) in a text file. I have a stock of functions which are sourced from... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Associative array index question

I am trying to assign indexes to an associative array in a for loop but I have to use an eval command to make it work, this doesn't seem correct I don't have to do this with regular arrays For example, the following assignment fails without the eval command: #! /bin/bash read -d "\0" -a... (19 Replies)
Discussion started by: Riker1204
19 Replies

3. Shell Programming and Scripting

Using associative array for comparison

Hello together, i make something wrong... I want an array that contains information to associate it for further processing. Here is something from my bash... You will know, what I'm trying to do. I have to point out in advance, that the variable $SYSOS is changing and not as static as in my... (2 Replies)
Discussion started by: Decstasy
2 Replies

4. Shell Programming and Scripting

Morse Code with Associative Array

Continuing my quest to learn BASH, Bourne, Awk, Grep, etc. on my own through the use of a few books. I've come to an exercise that has me absolutely stumped. The specifics: 1. Using ONLY BASH scripting commands (not sed, awk, etc.), write a script to convert a string on the command line to... (22 Replies)
Discussion started by: ksmarine1980
22 Replies

5. Shell Programming and Scripting

Associative Array with more than one item per entry

Hi all I have a problem where i have a large list ( up to 1000 of items) and need to have 2 items pulled from it into variables in a bash script my list is like the following and I could have it as an array or possibly an external text file maintained separately. Every line is different and... (6 Replies)
Discussion started by: kcpoole
6 Replies

6. Shell Programming and Scripting

Associative array

I have an associative array named table declare -A table table="fruit" table="veggie" table="GT" table="eminem" Now say I have a variable returning the value highway How do I find corresponding value GT ?? (this value that I find (GT in this case) is supposed to be the name of a mysql... (1 Reply)
Discussion started by: leghorn
1 Replies

7. Shell Programming and Scripting

Help needed on Associative array in awk

Hi All, I got stuck up with shell script where i use awk. The scenario which i am working on is as below. I have a file text.txt with contents COL1 COL2 COL3 COL4 1 A 500 400 1 B 500 400 1 A 500 200 2 A 290 300 2 B 290 280 3 C 100 100 I could able to sum col 3 and col4 based on... (3 Replies)
Discussion started by: imsularif
3 Replies

8. Shell Programming and Scripting

How to create dynamically associative array in PHP?

Hi, I have an html page like this: <html> <body> <form action="test.php" method = "post"> Enter your name:<input name="search" type = "text" size ="40"> <br> Enter your age:<input name="age" type = "text" size ="20"> <input type = "submit" name="submit" value="search"> <input type =... (1 Reply)
Discussion started by: vanitham
1 Replies

9. Shell Programming and Scripting

Perl: Sorting an associative array

Hi, When using sort on an associative array: foreach $key (sort(keys(%opalfabet))){ $value = $opalfabet{$key}; $result .= $value; } How does it handle double values? It seems to me that it removes them, is that true? If so, is there a way to get... (2 Replies)
Discussion started by: tine
2 Replies

10. Shell Programming and Scripting

Associative Array

Hi, I am trying to make an associative array to use in a popup_menu on a website. Here is what i have: foreach $entr ( @entries ) { $temp_uid = $entr->get_value(uid); $temp_naam = $entr->get_value(sn); $s++; } This is the popup_menu i want to use it in. popup_menu(-name=>'modcon',... (4 Replies)
Discussion started by: tine
4 Replies
Login or Register to Ask a Question