Improving code by using associative arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Improving code by using associative arrays
# 1  
Old 03-27-2012
Improving code by using associative arrays

I have the following code, and I am changing it to

Code:
#!/bin/bash

hasArgumentCModInfile=0
hasArgumentSrcsInfile=0
hasArgumentRcvsInfile=0

OLDIFS="$IFS"
IFS="|="                # IFS controls splitting. Split on "|" and "=", not whitespace.
set -- $*               # Set the positional parameters to the command line arguments.
IFS="$OLDIFS"

while [ "$#" -gt 0 ]
do

  case "$1" in

  "--"[cC][mM][oO][dD][iI][fF]|\
  "--"[cC][mM][oO][dD]"-"[iI][nN][fF][iI][lL][eE])
    shift
    arg_cmodInfile="${1}"
    hasArgumentCModInfile=1
  ;;

  "--"[sS][rR][cC][sS][iI][fF]|\
  "--"[sS][rR][cC][sS]"-"[iI][nN][fF][iI][lL][eE]|\
  "--"[sS][oO][uU][rR][cC][eE][sS]"-"[iI][nN][fF][iI][lL][eE])
    shift
    arg_srcsInfile="${1}"
    hasArgumentSrcsInfile=1
  ;;

  "--"[rR][cC][vV][sS][iI][fF]|\
  "--"[rR][cC][vV][sS]"-"[iI][nN][fF][iI][lL][eE]|\
  "--"[rR][eE][cC][eE][iI][vV][eE][rR][sS]"-"[iI][nN][fF][iI][lL][eE])
    shift
    arg_rcvsInfile="${1}"
    hasArgumentRcvsInfile=1
  ;;

  *)
    arg_browseDir_fileLst="$arg_browseDir_fileLst ${1}"
    hasArgumentBDirFileLst=1
  ;;

  esac

  shift                 # Skip ahead to the next argument

done

if [ $hasArgumentCModInfile -eq 1 ]; then
  ...
elif  [ $hasArgumentCModInfile -eq 0 ];
  ...
fi

if [ $hasArgumentSrcsInfile -eq 1 ]; then
  ...
elif  [ $hasArgumentSrcsInfile -eq 0 ];
  ...
fi

if [ $hasArgumentRcvsInfile -eq 1 ]; then
  ...
elif  [ $hasArgumentRcvsInfile -eq 0 ];
  ...
fi

Basically I cut out the initializations in the beginning such as

Code:
 hasArgumentCModInfile=0
hasArgumentSrcsInfile=0
hasArgumentRcvsInfile=0

Also I would have a common array called hasArgument with the field only filled when the corresponding option is selected.

I want to simplify it using associative arrays for storing the options as follows:


Code:
#!/bin/bash

OLDIFS="$IFS"
IFS="|="                # IFS controls splitting. Split on "|" and "=", not whitespace.
set -- $*               # Set the positional parameters to the command line arguments.
IFS="$OLDIFS"

while [ "$#" -gt 0 ]
do

  case "$1" in

  "--"[cC][mM][oO][dD][iI][fF]|\
  "--"[cC][mM][oO][dD]"-"[iI][nN][fF][iI][lL][eE])
    shift
    value[cmodInfile]="${1}"
    hasArgument[cmodInfile]=true
  ;;

  "--"[sS][rR][cC][sS][iI][fF]|\
  "--"[sS][rR][cC][sS]"-"[iI][nN][fF][iI][lL][eE]|\
  "--"[sS][oO][uU][rR][cC][eE][sS]"-"[iI][nN][fF][iI][lL][eE])
    shift
    value[srcsInfile]="${1}"
    hasArgument[srcsInfile]=true
  ;;

  "--"[rR][cC][vV][sS][iI][fF]|\
  "--"[rR][cC][vV][sS]"-"[iI][nN][fF][iI][lL][eE]|\
  "--"[rR][eE][cC][eE][iI][vV][eE][rR][sS]"-"[iI][nN][fF][iI][lL][eE])
    shift
    value[rcvsInfile]="${1}"
    hasArgument[rcvsInfile]=true
  ;;

  *)
    value[bdFileLst]="${value[bdFileLst]} ${1}"
    hasArgument[bdFileLst]=true
  ;;

  esac

  shift                 # Skip ahead to the next argument

done

if ${hasArgument[CModInfile]}; then     # User set value for CModInfile
  ...
elif  ! ${hasArgument[CModInfile]}; then  # User did not select CModInfile option
  ...
fi

if  ${hasArgument[SrcsInfile]}; then
   ...
 elif  ! ${hasArgument[SrcsInfile]}; then
   ...
 fi
 
if $hasArgument[RcvsInfile]}; then
   ...
 elif  ! ${hasArgument[RcvsInfile]}; then
   ...
 fi

I would be very grateful on some comments concerning this scheme, whether it is a good idea or further improvements.
# 2  
Old 03-27-2012
@kristinu
I could be off track because it is hard to follow your code, but have you read up on the Shell getopts command? It is designed to turn command line parameters into Shell variables in an orderly manner.
Just for interest, what is an "associative array" ?
# 3  
Old 03-27-2012
Quote:
Originally Posted by methyl
@kristinu
I could be off track because it is hard to follow your code, but have you read up on the Shell getopts command? It is designed to turn command line parameters into Shell variables in an orderly manner.
Just for interest, what is an "associative array" ?
As example,

Code:
prog.bash --chmodif=file.cmod

In this case the user has set the cmod file. Then
Quote:
hasArgument[cmodInfile] is true
and value[cmodInfile] is set to file.cmod
getopts does not handle long options. Associative arrays let you return different values for different strings, rather than just numbered positions in the array. Associative arrays becomes quite simple like a simple array but instead of an index, one has a string to distinguish elements.
# 4  
Old 03-27-2012
Thank you for your explanation of "associative array". In the context of unix Shell scripting I do not understand it at all ... but in the context of Database Administration I do understand.

Let me leave this thread to those posters who understand what kristinu is trying to achive.

Bye.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Improving code

Gents, I did the below code to get an output (report) ,.. the code works fine but I believe it can be more shorted using better method. Please if you can help, to generate same output improving the code , will be great. here my code. # get diff in time awk '{$9=$8-prev8;prev8=$8;print... (8 Replies)
Discussion started by: jiam912
8 Replies

2. Shell Programming and Scripting

Associative arrays awk

Hi, I have the following dataset. A 2 1 272 A 2 2 333 A 2 3 222 A 3 1 222 A 3 2 11 B 1 1 112 B 1 2 998 B 2 1 667 C 1 1 887 C 1 2 887 C 2 1 998 I need to have an associate array based on the first column and generate a auto generated number column in the last column. Needed output:... (2 Replies)
Discussion started by: mitt
2 Replies

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

4. UNIX for Dummies Questions & Answers

Using associative arrays with an if statement

I have this piece of code. The first if statement is not working, however the second if statement is working fine. I have set a value for Srcs to be file.srcs and want to print it. If no value for Rcvs is set, I get the print statement correctly hasValue="file.srcs" if ${hasValue}; then ... (0 Replies)
Discussion started by: kristinu
0 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. Shell Programming and Scripting

associative arrays?

Hello, i'm writing a little script that checks a .txt file for a specific ID that came after 9:10 am which outputs it's data to a file LateUsers.txt once done , it should mention the following: Number of late users Number of unique late users Over all late users percentage number of... (0 Replies)
Discussion started by: rollyah
0 Replies

7. Shell Programming and Scripting

Associative arrays

Hi all, #!/usr/dt/bin/dtksh typeset -A wavelength wavelength=650 wavelength=590 wavelength=510 wavelength=475 wavelength=445 wavelength=400 I have created an associative array like the one above. Now I am trying to print the values If i give print ${wavelength} it is... (4 Replies)
Discussion started by: prasperl
4 Replies

8. UNIX for Dummies Questions & Answers

Unable to understand associative nature of awk arrays

About associative nature of awk arrays i'm still confused, not able to understand yet how array element can be accessed based on a string, I got one example at gawk manual to illustrate associative nature of awk arrays, it goes here: Codeawk ' # Print list of word frequencies { for (i = 1;... (3 Replies)
Discussion started by: nervous
3 Replies

9. UNIX for Dummies Questions & Answers

are Associative Arrays possible in UNIX?

Is it possible to say.. myArr=34 myArr=15 ? (11 Replies)
Discussion started by: yongho
11 Replies

10. Shell Programming and Scripting

improving my script

Hi; I want to access our customer database to retreive all clients that have as language index 2 or 3 and take their client number. My input is a file containing all client numbers. i access the data base using a function call "scpshow". The total number of clients i want to scan is 400 000... (6 Replies)
Discussion started by: bcheaib
6 Replies
Login or Register to Ask a Question