Storing values in arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing values in arrays
# 1  
Old 11-03-2010
Storing values in arrays

I have the following csh script which lets the use pass the following as an argument

-legend=tag1/tag2/tag3/tag4/tag5/tag6/tag7

We pass a number of tags separated by '/'. I want to save the legend tags in an array and have done as below. How can I improve on this as things are getting quite messy. This is only a short part of the script.

Code:
#!/bin/csh

  set iarg = 0

  set optlegend = 0
  set nf = 0

  set Version = "V04"
  alias MATH 'set \!:1 = `echo "\!:3-$" | bc -l`'

  set narg = $#argv
  while ($iarg < $narg)

    MATH iarg = $iarg + 1
    set arg = $argv[$iarg]
    set opt = ` echo $arg | awk 'BEGIN { FS="=" } { print $1 }'  \
                          | tr "[A-Z]" "[a-z]" `
    set par = ` echo $arg | awk 'BEGIN { FS="=" } { print $2 }' `

    switch ($opt)
    case "-legend":
        set Alegend = $par
        set legend = $Alegend
        breaksw
    default:
      set filename = `echo $arg | awk 'BEGIN {FS="."} {print $1}'`
      set filextension = `echo $arg | awk 'BEGIN {FS="."} {print $2}'`
      set nxz = `echo $filename | awk 'BEGIN{FS="-"} {print $NF}'`
      echo "File = $filename"
      set fullnames = "$fullnames $arg"   # Store the file names
      set fnames = "$fnames $filename"   # Store the file names
      set fext = "$fext $filextension"   # Store the file extensions
      MATH nf = $nf + 1
    endsw

  end   # while

  set fin = ($fnames)
  set fout = "fout"

    if ($nf == 1) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend = ($legend1)
      echo " $nf. $legend"
    else if ($nf == 2) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend = ($legend1 $legend2)
      echo " $nf. $legend"
    else if ($nf == 3) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend3 = `echo $Alegend | awk '{split($1,a,"/"); print a[3]}'`
      set legend = ($legend1 $legend2 $legend3)
      echo " $nf. $legend"
    else if ($nf == 4) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend3 = `echo $Alegend | awk '{split($1,a,"/"); print a[3]}'`
      set legend4 = `echo $Alegend | awk '{split($1,a,"/"); print a[4]}'`
      set legend = ($legend1 $legend2 $legend3 $legend4)
      echo " $nf. $legend"
    else if ($nf == 5) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend3 = `echo $Alegend | awk '{split($1,a,"/"); print a[3]}'`
      set legend4 = `echo $Alegend | awk '{split($1,a,"/"); print a[4]}'`
      set legend5 = `echo $Alegend | awk '{split($1,a,"/"); print a[5]}'`
      set legend = ($legend1 $legend2 $legend3 $legend4 $legend5)
      echo " $nf. $legend"
    else if ($nf == 6) then
      set fname = $fsmp6
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend3 = `echo $Alegend | awk '{split($1,a,"/"); print a[3]}'`
      set legend4 = `echo $Alegend | awk '{split($1,a,"/"); print a[4]}'`
      set legend5 = `echo $Alegend | awk '{split($1,a,"/"); print a[5]}'`
      set legend6 = `echo $Alegend | awk '{split($1,a,"/"); print a[6]}'`
      set legend = ($legend1 $legend2 $legend3 $legend4 $legend5  \
                    $legend6)
      echo " $nf. $legend"
    else if ($nf == 7) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend3 = `echo $Alegend | awk '{split($1,a,"/"); print a[3]}'`
      set legend4 = `echo $Alegend | awk '{split($1,a,"/"); print a[4]}'`
      set legend5 = `echo $Alegend | awk '{split($1,a,"/"); print a[5]}'`
      set legend6 = `echo $Alegend | awk '{split($1,a,"/"); print a[6]}'`
      set legend7 = `echo $Alegend | awk '{split($1,a,"/"); print a[7]}'`
      set legend = ($legend1 $legend2 $legend3 $legend4 $legend5  \
                    $legend6 $legend7)
      echo " $nf. $legend"
    else if ($nf == 8) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend3 = `echo $Alegend | awk '{split($1,a,"/"); print a[3]}'`
      set legend4 = `echo $Alegend | awk '{split($1,a,"/"); print a[4]}'`
      set legend5 = `echo $Alegend | awk '{split($1,a,"/"); print a[5]}'`
      set legend6 = `echo $Alegend | awk '{split($1,a,"/"); print a[6]}'`
      set legend7 = `echo $Alegend | awk '{split($1,a,"/"); print a[7]}'`
      set legend8 = `echo $Alegend | awk '{split($1,a,"/"); print a[8]}'`
      set legend = ($legend1 $legend2 $legend3 $legend4 $legend5  \
                    $legend6 $legend7 $legend8)
      echo " $nf. $legend"
    else if ($nf == 9) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend3 = `echo $Alegend | awk '{split($1,a,"/"); print a[3]}'`
      set legend4 = `echo $Alegend | awk '{split($1,a,"/"); print a[4]}'`
      set legend5 = `echo $Alegend | awk '{split($1,a,"/"); print a[5]}'`
      set legend6 = `echo $Alegend | awk '{split($1,a,"/"); print a[6]}'`
      set legend7 = `echo $Alegend | awk '{split($1,a,"/"); print a[7]}'`
      set legend8 = `echo $Alegend | awk '{split($1,a,"/"); print a[8]}'`
      set legend9 = `echo $Alegend | awk '{split($1,a,"/"); print a[9]}'`
      set legend = ($legend1 $legend2 $legend3 $legend4 $legend5  \
                    $legend6 $legend7 $legend8 $legend9)
      echo " $nf. $legend"
    else if ($nf == 10) then
      set legend1 = `echo $Alegend | awk '{split($1,a,"/"); print a[1]}'`
      set legend2 = `echo $Alegend | awk '{split($1,a,"/"); print a[2]}'`
      set legend3 = `echo $Alegend | awk '{split($1,a,"/"); print a[3]}'`
      set legend4 = `echo $Alegend | awk '{split($1,a,"/"); print a[4]}'`
      set legend5 = `echo $Alegend | awk '{split($1,a,"/"); print a[5]}'`
      set legend6 = `echo $Alegend | awk '{split($1,a,"/"); print a[6]}'`
      set legend7 = `echo $Alegend | awk '{split($1,a,"/"); print a[7]}'`
      set legend8 = `echo $Alegend | awk '{split($1,a,"/"); print a[8]}'`
      set legend9 = `echo $Alegend | awk '{split($1,a,"/"); print a[9]}'`
      set legend10 = `echo $Alegend | awk '{split($1,a,"/"); print a[10]}'`
      set legend = ($legend1 $legend2 $legend3 $legend4 $legend5  \
                    $legend6 $legend7 $legend8 $legend9 $legend10)
      echo " $nf. $legend"
    endif

  endif


Last edited by kristinu; 11-03-2010 at 12:12 PM..
# 2  
Old 11-03-2010
Google csh array tutorial?

http://www-cs.canisius.edu/ONLINESTU...ramming.html#M

PS: Why csh? Class work, or never considered bash or ksh?
# 3  
Old 11-03-2010
Some other guy wrote it and I am trying to use the program. There will be lot of stuff to change if I want to convert everything to bash or ksh.
# 4  
Old 11-03-2010
Something like this turns all the slashes into spaces into an array if I read my tutorial correctly:
Code:
 
legends =(`echo Alllegends | tr '/' ' '`)

Then, solve the problem with iteration.
http://www.eng.hawaii.edu/Tutor/csh.html#control
Code:
foreach legend ( `echo Alllegends | tr '/' ' '`)
 .
 .
 .
end

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

storing multiple values in a array variable

Am using a find command in my script .The output may be one or more. I need to store those values in a array and need to access those. Am unable to find the solution . Any help on this will be helpful. if < code> else a=<find command output which gives the file name either 1 or more> if 1... (1 Reply)
Discussion started by: rogerben
1 Replies

2. UNIX for Dummies Questions & Answers

Storing values in shell variable

Hi, I am writing a shell script where, x=y y=z When I want to print z, I can do $y How do I use only "x" without any direct reference to "y" to print z? Thanks, -G (3 Replies)
Discussion started by: gaurab
3 Replies

3. Shell Programming and Scripting

Storing values in arrays using csh

I am reading a number of files but then I want to put the ranges xmin xmax ymin ymax as arrays for each file. Any idea how I can do this??? set j = 1 echo "Welcome $i times" while ( $j <= $i ) echo "$j" set fname = $fin-bst-misf.xy echo " "$fname ... (0 Replies)
Discussion started by: kristinu
0 Replies

4. Shell Programming and Scripting

storing values in arrays using shell

Friends, I have to execute a command and store its contents into an array using shell. this is what i have tried #!/bin/bash disk_names = ($(`iostat -xtc | egrep -v "device|nfs" | awk '{print $1}'| tr '\n' ' ' `)) But its throwing an error message as ./test-script ./test-script:... (6 Replies)
Discussion started by: achak01
6 Replies

5. Shell Programming and Scripting

fetching values using awk and storing into array

hi all I am using awk utility to parse the file and fetching two different vaues from two different record of a record set. I am able to see the result, now i want to store the result and perform some check of each values form database to mark valid and invalid. could you please help me... (3 Replies)
Discussion started by: singhald
3 Replies

6. Shell Programming and Scripting

storing values in a list or array

i have a file called file.txt having the following entries. 2321 2311 2313 4213 i wnat to store these values in a list and i want to iterate the list using loop and store it in another list (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

7. UNIX for Dummies Questions & Answers

Storing Multiple Values in a Variable

Hi, My code is as below: integer i=7 while ((i <= 5 )); do # test_out is a variable which contains data separated by "^". a= `echo $test_out | cut -d"^" -f$i` echo "$a" (( i = i + 1)); done From the above code, i kept $i after f so that i can capture all the data which is... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

8. Shell Programming and Scripting

Storing Command Line Values

Hi, I am trying to read the value passed by the user and store it in a variable so that later I can read it from the variable. But I am getting errors. Can you please help? Thanks. Code: $ECHO "Enter the Country for which you want the installation to be executed? (US/India):" read COUNTRY... (3 Replies)
Discussion started by: yoursdavinder
3 Replies

9. Shell Programming and Scripting

Storing information in arrays....

Hello, I am writing a shell script to do the following. It greps information from the messages log and then I use the cut command to isolate the field I need (the username) and output the information to a text file. I now have to do the following. Count how many times each user logged in. So... (3 Replies)
Discussion started by: mojoman
3 Replies

10. Shell Programming and Scripting

Storing values in variable

Hi All, Here is the description of the problem. I am scripting for database access using k-shell on solaris box dbaccess <databasename> - << EOF 2>/dev/null | awk 'BEGIN {FS=" "}\ {printf "%s", $1}' | grep -v "^$" | \ read cnt1 OUTPUT TO PIPE cat WITHOUT HEADINGS select count(*) from... (1 Reply)
Discussion started by: matrixmadhan
1 Replies
Login or Register to Ask a Question