Multidimensions array problem 
Author Message
 Multidimensions array problem

I need to parse a file and create an associative multi dimension array
from it.

A simple record would be:

[Account]
PRIV=3
LEVEL=5

The array I want would look like:
The 1st part of the array is associated with the account name.
And each entry in the array would have 3 subscript: name, priv and
level.

array[Account, name]=Account
array[Account, priv]=3
array[Account, level]=5

Here is how I'm trying to do it:

#!/bin/sh
awk '
BEGIN { RS="" ; FS="\n" ; ORS="\n\n" ; flag=0 }

/^\[/ { for (i=1;i<=NF;i++) {
        if($i ~ /^\[/) {
            acct=substr($0,2,length($1)-2)
            flag++
        }

        if($i ~ /^PRIV/) {
                split($i,priv,"=")
        }
    }

account[acct, name]=acct
account[acct, pri]=priv[2]

Quote:
}

END {  for (j in account) {
                split(j, acctmp, SUBSEP)
                    for (k in acctmp)
                        print acctmp[k]
Quote:
}
     print flag
} ' tusacct.scp

exit 0

But it does not work.

I need it so I can use this array in another awk call within the same
shell script.
I need the PRIV and LEVEL associated with the name from the 1st file
because they are not in the 2nd. Only the name is in both.

(and it's not a school homework :))

Thanks in advance for any infos!



Fri, 01 Feb 2002 03:00:00 GMT  
 Multidimensions array problem

Quote:

> I need to parse a file and create an associative multi dimension
> array from it.  A simple record would be:
> [Account]
> PRIV=3
> LEVEL=5
> The array I want would look like: The 1st part of the array is
> associated with the account name.  And each entry in the array would
> have 3 subscript: name, priv and level.
> array[Account, name]=Account
> array[Account, priv]=3
> array[Account, level]=5

I didn't try to analyze your code, but here's one quck and direty
solution:

BEGIN{FS="="}
/^\[.*\]/{x=$0;gsub(/[\[\]]/,"",x);next}
{if (NF==2) {array[x,$1]=$2}}
END{for (x in array) {split( x, arrtmp,  SUBSEP);
                      print arrtmp[1] ":", arrtmp[2] "=" array[x]}}
#EOF

--%!PS
10 10 scale/M{rmoveto}def/R{rlineto}def 12 45 moveto 0 5 R 4 -1 M 5.5 0 R
currentpoint 3 sub 3 90 0 arcn 0 -6 R 7.54 10.28 M 2.7067 -9.28 R -5.6333
2 setlinewidth 0 R 9.8867 8 M 7 0 R 0 -9 R -6 4 M 0 -4 R stroke showpage
       % Henry Churchyard         http://www.crossmyt.com/hc/



Fri, 01 Feb 2002 03:00:00 GMT  
 Multidimensions array problem

% A simple record would be:
%
% [Account]
% PRIV=3
% LEVEL=5

I've seen files like that before. As a hack, you might want to try making
 FS = "[]=[]"
and then test for NF == 3 or NF == 2.

% And each entry in the array would have 3 subscript: name, priv and
% level.

OK, but the way you've put them in the script below, these are all variables,
which evaluate to "" since nothing's been assigned to them.

[...]

% account[acct, name]=acct
% account[acct, pri]=priv[2]

You should initialise name and pri to some values (ie, name="name" and
pri="priv"). I think that will sort out your problem.
--

Patrick TJ McPhee
East York  Canada



Sat, 02 Feb 2002 03:00:00 GMT  
 Multidimensions array problem

Quote:

> I need to parse a file and create an associative multi dimension array
> from it.

> A simple record would be:

> [Account]
> PRIV=3
> LEVEL=5

> The array I want would look like:
> The 1st part of the array is associated with the account name.
> And each entry in the array would have 3 subscript: name, priv and
> level.

> array[Account, name]=Account
> array[Account, priv]=3
> array[Account, level]=5

> Here is how I'm trying to do it:

> #!/bin/sh
> awk '
> BEGIN { RS="" ; FS="\n" ; ORS="\n\n" ; flag=0 }

> /^\[/ { for (i=1;i<=NF;i++) {

Why do you need a for statement ? Awk executes tusacct.scp file line by
line anyway.

Quote:

>         if($i ~ /^\[/) {
>             acct=substr($0,2,length($1)-2)
>             flag++
>         }

>         if($i ~ /^PRIV/) {
>                 split($i,priv,"=")
>         }

Why above condition is inside for loop which has a pattern match before
it.
The line starting with PRIV will never enter this loop as the pattern
before for is not met.

Quote:
> <snip>

The following is the pseudocode for your problem. It is to be coded as
body of the awk. As a C programmer still I prefer using if. You can
replace it with simple pattern
match.

if ( index($0,"[") == 1 ) {
     # Record starting with Account Name
      Acct = substr( $0,2,length($0) -2 );
      AcctFoundFlag = 1;

Quote:
}

elsif ( ($0 == "^PRIV" && ( AcctFoundFlag == 1 )) {
      RetVal  = split( $0,priv,"=" );
      AcctFoundFlag = 2;
Quote:
}elsif ( ($0 == "^LEVEL" && ( AcctFoundFlag == 2 )) {

      RetVal  = split( $0,level,"=" );
      AcctFoundFlag = 3;
Quote:
}else {

  print "Error in Input Record"
  exit

Quote:
}

if ( AcctFoundFlag == 3 ) { # All Record Entries are received
    array[Account, name]=Account
    array[Account, priv]=priv[2]
    array[Account, level]=level[2]
    AcctFoundFlag = 0;

Quote:
}

Use AcctFoundFlag and RetVal to check Error Conditions.


Sat, 02 Feb 2002 03:00:00 GMT  
 Multidimensions array problem


Quote:


>> I need to parse a file and create an associative multi dimension array
>> from it.

>> A simple record would be:

>> [Account]
>> PRIV=3
>> LEVEL=5

>> The array I want would look like:
>> The 1st part of the array is associated with the account name.
>> And each entry in the array would have 3 subscript: name, priv and
>> level.

>> array[Account, name]=Account
>> array[Account, priv]=3
>> array[Account, level]=5

 It looks like you could get away with 2 arrays: Priv and Level, both
 "indexed" by Account, thusly:
  Priv[Account] = 3;  Level[Account] = 5
  and maybe a third array -- Accts -- containing accounts themselves, say,
  to do something like:
  for( Account in Accts )
     { print "Account = ",Account
       print "PRIV = ",Priv[Account]
       print "LEVEL = ",Level[Account]
     }


Mon, 04 Feb 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Overloading array arg for multidimensions/RESHAPE efficiency

2. set value for multidimension array

3. Looping through a multidimension array

4. Problem with with Array of U8 to Array of Array of Boolean

5. Difficult problem to solve: Tcl 7.3 to Tcl 8.0p2 Array of Array Problem

6. Dbase to Clarion w/ multidimensions

7. multidimension variables

8. Problem with arrays of arrays (matrices) copying

9. An array of records within an array of records (Still a problem)

10. Array problem - adjust-array

11. Problems using an array within another array

12. convert 2d array to 1d array without using shift registers and build array

 

 
Powered by phpBB® Forum Software