The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Is it possible to draw table/matrix using shell script? jakSun8 Shell Programming and Scripting 0 03-13-2008 06:53 AM
Creating a Hash Table callmetheskull High Level Programming 4 12-23-2007 11:17 AM
hash table implementations in C Language vlrk High Level Programming 11 07-15-2007 06:33 AM
Alter Table Shell Script ankitgupta Shell Programming and Scripting 2 10-06-2006 03:01 AM
Reading a table in a shell script luiscarvalheiro Shell Programming and Scripting 13 08-10-2006 03:16 PM

Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-22-2003
Registered User
 

Join Date: Oct 2001
Location: Bangalore
Posts: 93
Creating a hash table using shell script

Hi,

For one of my programs, I need to have a hashtable as in Perl. Unfortunately shell doesnt provide any variable like hash. Is there anyway/trick, I could implement a hash in shell (using shell scripts/sed/awk).

JP
Forum Sponsor
  #2 (permalink)  
Old 10-28-2003
RTM's Avatar
RTM RTM is offline
Hog Hunter
 
Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
See the two Shell Array Variable scripts at context-switch.com - may be helpful.
  #3 (permalink)  
Old 10-29-2003
Registered User
 

Join Date: Oct 2003
Location: United Kingdom
Posts: 37
Faked Hash

You could fake a hash using a pseudo-associative array. AWK provides this facility but it involves a little work in the ksh, for example:

Storing a value using a variable as a key:

where n="TEST"

typeset val_${n}=3

the value of ${val_TEST} will be seen to be 3, checked using set or env. Reading the value is a little trickier. It can be performed directly if the key name is constant:

echo "${val_TEST}"

However, to use a variable key, perform the following:

echo "$(eval echo \$val_${n})"

This acts as a pointer and should then return 3.

In order to run the hash, set or env can be used along with a pattern matching utility, i.e. grep or egrep, especially if the variable name prefix is very specific (i.e. val_).

Last edited by Simerian; 10-29-2003 at 04:10 AM.
Google UNIX.COM
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0