Find the answer to your Linux question:
Results 1 to 2 of 2
Hello all, I am new to this forum. I am trying to create a shell script to automate a process that takes inputs from two sets of data. The variable ...
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    8

    Irregular 2-d array in shell script

    Hello all,

    I am new to this forum. I am trying to create a shell script to automate a process that takes inputs from two sets of data. The variable SHELL in my system echoes as /bin/tcsh, but I see that my /bin directory has other possibilities, such as bash. So, if changing the shell could simplify my problem, then I would go with that.

    I have a list of files containing data about objects, say

    obj1.dat, obj2.dat, .... objn.dat, where n is around 20 (it varies).

    These objects belong to a few different categories, and there are files detailing the properties of the categories, say

    cat1.prop, cat2.prop, ... catm.prop, where m is smaller than n (more than one object in each category).

    I know that shell scripts can handle 2-d arrays, although I am not sure how. The thing here is that category 1 could have 5 objects and category 3 could have 7 objects. So, this could produce a 2-d array, but not a rectangular one.

    I need to process these files taking into acount the data about the object and the properties of the category the object belongs to. Here is what I tried to do:

    # First I enter a list of category names and m lists of objects in each category

    category='cat1 cat2 cat3 ... catm'

    cat1-obj='obj1 obj2 obj3'
    cat2-obj='obj4 obj5'
    cat3-obj='obj6 obj7 obj8 obj9 obj10'
    .
    .
    .
    catm-obj='objx objy objz objn'

    # The lines above show the irregular 2-d array. Then I try to trick the script into parsing through this irregular 2-d array:

    for i in $category
    do
    data='$i -obj'
    for j in $data
    do
    mycommand $j.dat $i.prop > $j-$i.out
    done
    done

    The line "mycommand $j.dat ..." is just the line that processes the information. That has been tested to work fine. Feeding the information to this is what does not work. The lines " data='$i-obj' " and " for j in $data " do not have the desired effect. I have tried other ways, like data=`echo $i "-obj"` and things of the sort, by to no avail.

    Thanks in advance for any advice.

    Otto.

  2. #2
    Just Joined!
    Join Date
    Aug 2010
    Posts
    8
    I could try to reword my question. I would like to type in the information:

    categories='cat1 cat2 cat3 ... catm'

    cat1='obj1 obj2 obj3'
    cat2='obj4 obj5'
    cat3='obj6 obj7 obj8 obj9'
    .
    .
    .
    catm='objx objy objz'

    And have the script echo the list

    obj1 cat1
    obj2 cat1
    obj3 cat1
    obj4 cat2
    obj5 cat2
    obj6 cat3
    obj7 cat3
    .
    .
    .
    objz catm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...