-3

the file is like this

ATOM      1 HO5'   C     1       3.429  -7.861   3.641  1.00  0.00           H  
ATOM      2  O5'   C     1       4.232  -7.360   3.480  1.00  0.00           O  
ATOM      3  C5'   C     1       5.480  -8.064   3.350  1.00  0.00           C  
ATOM      4  H5'   C     1       5.429  -8.766   2.518  1.00  0.00           H  

I want to store these into 2D array, do some calculation with values and retrieve later I declared arrays as follows,

I split the file as follows,

readarray row < sample.pdb

IFS=' \t\r\n' read -a cell <<< "$row"

    for (( r = 0; r <= ${#row[@]}; r++ ))      # row
    do

      for (( c = 0 ; c <= ${#cell[@]}; c++ )) #cells
        do
             echo ${cell[$c]}

            declare -A matrix[$r,$c]="${cell[$c]}"

        done

    done

and trying to retrieve as follows

    for ((j=0; j<=${#row[@]}; j++))
    do

        for ((i=0; i<=${#cell[@]}; i++))
        do

            echo  ${matrix[$j,$i]}

        done

    done

Expected output

ATOM      
1 
HO5'   
C     
1       
3.429  
-7.861   
3.641  
1.00  
0.00           
H  
ATOM      
2  
O5'   
C     
1       
4.232  
-7.360   
3.480  
1.00  
0.00           
O  
AND THE OTHER LINE

but the output is (first line repeat again and again)

ATOM 1 HO5' A 1 3.429 -7.861 3.641 1.00 0.00 H

ATOM 1 HO5' A 1 3.429 -7.861 3.641 1.00 0.00 H

I would like to know the appropriate 2D dimensional splitting.

Hutch
  • 167
  • 2
  • 8
  • 1
    A good place to start would be using `declare -p matrix` to print the current state of your array. If content was loaded correctly, you can remove the code that tries to do the load and ask only about the problem _using_ that content. If it was loaded wrongly, you can remove all the code that tries to read the content, and focus only on the problem loading. Either way, the question can and should be focused on a specific, narrow problem. – Charles Duffy Jun 26 '20 at 18:35
  • ...you can see the code at https://ideone.com/2BGNWE showing the result of that `declare -p matrix` (at the bottom of the output); but it's not obvious, without knowing what the rationale and intent was behind your logic, whether that's correct or not and *why* it's correct or not. – Charles Duffy Jun 26 '20 at 18:36
  • 2
    Frankly, just having your expected output is not so helpful without an adequate explanation of _why_ that output should be correct. `echo ${matrix[$i,$j]}` will *never* put more than one cell on the same line, so there's no reason to ever expect the code to print your stated expected output. – Charles Duffy Jun 26 '20 at 18:38
  • this is a file format called .pdb which store chemistry molecular dynamic data, ATOM should be the first term in the line, and it is missing cannot figure out why. – Hutch Jun 26 '20 at 18:42
  • The `(.)` (parens) on your `printf` sample aren't helping. for the shells printf, the usage iis `printf "%sFMTSTR%setc%detc\n" "$1" "$2" "$3"`. Or did you mean using `awk`, which would accept `printf() and would really be the good tool for this problem. Note that "$1"` etc are the cmd-line args, not the fields in the data file. Good luck. – shellter Jun 26 '20 at 18:49
  • the first field (`ATOM`) is missing because bash array indexes start with 0/zero, while your code is starting with index=1 (ie, you're skipping index=0); you can see this in the output from `typeset -p cell` – markp-fuso Jun 26 '20 at 23:34

1 Answers1

1

Bash do not have a multi-dimensional array.

associative array pretending to be used as a multi-dimensional array:

this codes works

readarray row < file.pdb

declare -A matrix
IFS=' \t\r\n' read -a cell <<< $row


num_rows=${#row[@]}
num_cells=${#cell[@]}

echo ${#row[@]}
echo ${#cell[@]}

for ((i=0; i<num_rows; i++)) do

IFS=' \t\r\n' read -a cell <<< ${row[$i]}

    for ((j=0; j<num_cells; j++)) do
    
       
        matrix[$i,$j]=${cell[$j]}
    done
done

f1=" %5s"
f2="%$((${#num_rows}+1))s"


for ((i=0;i<=num_rows;i++)) do

    
    for ((j=0;j<=num_cells;j++)) do
    
        printf "$f1" ${matrix[$i,$j]}
        
    done
    echo
done

output

4
11
  ATOM     1  HO5'     A     1 3.429 -7.861 3.641  1.00  0.00     H      
  ATOM     2   O5'     G     1 4.232 -7.360 3.480  1.00  0.00     O      
  ATOM     3   C5'     C     1 5.480 -8.064 3.350  1.00  0.00     C      
  ATOM     4   H5'     U     1 5.429 -8.766 2.518  1.00  0.00     H  
Robert Longson
  • 118,664
  • 26
  • 252
  • 242
Hutch
  • 167
  • 2
  • 8