From: yilmaz on
Hi, Please help me with the following problem. I have very little
experience with multidimensional arrays and need your advice.

I have several lists like below from files comp-1, comp-2, and comp-3:

{ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33} {hj 0.11} {jj
0.345} {gg 0.6}
{DW} inout {ft 0.13} {fg 0.19} {df 0.5} {gh 0.2} {hj 0.14} {jj
0.53} {gg 0.31}
.....

that will be converted into a table like below:

------------------------------------------------------------------------------------------------------------------------
| Comp-1 |
Comp-2 | Comp-3
Items ------------------------------------
|------------------------- |---------------------------------
| ft fg df gh | ft fg
df gh | ft fg df gh
--------------------------------------------------
|--------------------------------------------------------------
ADA | 0.23 0.9 0.45 0.33 | 0.3 0.4 0.1 0.2 |
0.22 0.44 0.66 0.8
DW | 0.13 0.19 0.5 0.2 | 0.1 0.2 0.5 0.6
| 0.11 0.12 0.3 0.91

............................................................................................................................
.....
# more rows with data

This table will first be created in an XML-like structure and will be
displayed by the application as a table above.

I see an obvious pattern for using multidimensional arrays but not
sure how to do this row-by row to populate the XML-tree ....

For example, if I have the file comp-1 and the first row of the table
{ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33}

Do I have to read those all individual items into the array one by
one?

set row(comp1,ADA,ft) 0.23
set row(comp1,DW,ft) 0.13
.....

and then write this into the XML file into appropriate tags?

Please advise.

Cheers!
-Yil



From: yilmaz on
On Jun 12, 2:37 pm, yilmaz <yyyyil...(a)gmail.com> wrote:
> Hi, Please help me with the following problem. I have very little
> experience with multidimensional arrays and need your advice.
>
> I have several lists like below from files comp-1, comp-2, and comp-3:
>
> {ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33} {hj 0.11} {jj
> 0.345} {gg 0.6}
> {DW} inout {ft 0.13}  {fg 0.19}   {df 0.5}   {gh 0.2}   {hj 0.14}  {jj
> 0.53}  {gg 0.31}
> ....
>
> that will be converted into a table like below:
>
> ------------------------------------------------------------------------------------------------------------------------
>               |         Comp-1                    |
> Comp-2              | Comp-3
> Items      ------------------------------------
> |-------------------------    |---------------------------------
>               |   ft       fg     df           gh    |  ft      fg
> df   gh   | ft        fg       df     gh
> --------------------------------------------------
> |--------------------------------------------------------------
> ADA       |   0.23     0.9    0.45    0.33  |  0.3    0.4  0.1 0.2   |
> 0.22   0.44   0.66  0.8
> DW        |   0.13     0.19   0.5     0.2    |  0.1    0.2  0.5 0.6
> | 0.11   0.12   0.3   0.91
>
> ............................................................................................................................
> ....
> # more rows with data
>
> This table will first be created in an XML-like structure and will be
> displayed by the application as a table above.
>
> I see an obvious pattern for using multidimensional arrays but not
> sure how to do this row-by row to populate the XML-tree  ....
>
> For example, if I have the file comp-1 and the first row of the table
> {ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33}
>
> Do I have to read those all individual items into the array one by
> one?
>
> set row(comp1,ADA,ft) 0.23
> set row(comp1,DW,ft) 0.13
> ....
>
> and then write this into the XML file into appropriate tags?
>
> Please advise.
>
> Cheers!
> -Yil

A better example of the output table:

Comp-1 Comp-2 Comp-3
ft fg df gh ft fg df gh ft
fg df gh
ADA 0.23 0.9 0.45 0.33 0.3 0.4 0.1 0.2 0.22
0.44 0.66 0.8
DW 0.13 0.19 0.5 0.2 0.1 0.2 0.5 0.6 0.11
0.12 0.3 0.91

......
......
From: tom.rmadilo on
On Jun 12, 2:47 am, yilmaz <yyyyil...(a)gmail.com> wrote:
> On Jun 12, 2:37 pm, yilmaz <yyyyil...(a)gmail.com> wrote:
>
>
>
>
>
> > Hi, Please help me with the following problem. I have very little
> > experience with multidimensional arrays and need your advice.
>
> > I have several lists like below from files comp-1, comp-2, and comp-3:
>
> > {ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33} {hj 0.11} {jj
> > 0.345} {gg 0.6}
> > {DW} inout {ft 0.13}  {fg 0.19}   {df 0.5}   {gh 0.2}   {hj 0.14}  {jj
> > 0.53}  {gg 0.31}
> > ....
>
> > that will be converted into a table like below:
>
> > --------------------------------------------------------------------------- ---------------------------------------------
> >               |         Comp-1                    |
> > Comp-2              | Comp-3
> > Items      ------------------------------------
> > |-------------------------    |---------------------------------
> >               |   ft       fg     df           gh    |  ft      fg
> > df   gh   | ft        fg       df     gh
> > --------------------------------------------------
> > |--------------------------------------------------------------
> > ADA       |   0.23     0.9    0.45    0.33  |  0.3    0.4  0.1 0.2   |
> > 0.22   0.44   0.66  0.8
> > DW        |   0.13     0.19   0.5     0.2    |  0.1    0.2  0.5 0.6
> > | 0.11   0.12   0.3   0.91
>
> > ............................................................................ ................................................
> > ....
> > # more rows with data
>
> > This table will first be created in an XML-like structure and will be
> > displayed by the application as a table above.
>
> > I see an obvious pattern for using multidimensional arrays but not
> > sure how to do this row-by row to populate the XML-tree  ....
>
> > For example, if I have the file comp-1 and the first row of the table
> > {ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33}
>
> > Do I have to read those all individual items into the array one by
> > one?
>
> > set row(comp1,ADA,ft) 0.23
> > set row(comp1,DW,ft) 0.13
> > ....
>
> > and then write this into the XML file into appropriate tags?
>
> > Please advise.
>
> > Cheers!
> > -Yil
>
> A better example of the output table:
>
>                   Comp-1            Comp-2                  Comp-3
>          ft    fg     df    gh      ft     fg   df   gh     ft
> fg     df    gh
> ADA      0.23  0.9    0.45  0.33    0.3    0.4  0..1  0.2    0.22
> 0.44   0.66  0.8
> DW       0.13  0.19   0.5   0.2     0.1    0.2  0.5  0.6    0.11
> 0.12   0.3   0.91
>
> .....
> .....

If it is a multi-dimensional array which you want to access like one,
use array keys as a list:

% set Array([list Comp-1 ADA ft]) .23

then to retrieve the data:

% set Array([list Comp-1 ADA ft])
..23

Or loop through the entire set:

% set X [list Comp-1 Comp-2 ...]
% set Y [list ADA DW ...]
% set Z [list ft fg df gh ...]

% foreach x $X {
% foreach y $Y {
% foreach z $Z {
% set val [set Array([list $x $y $z])]
% # do something with x, y, z and val
% } } }

This also works with sparse arrays
From: Sean Woods on
On Jun 12, 5:37 am, yilmaz <yyyyil...(a)gmail.com> wrote:
> Hi, Please help me with the following problem. I have very little
> experience with multidimensional arrays and need your advice.
>
> I have several lists like below from files comp-1, comp-2, and comp-3:
>
> {ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33} {hj 0.11} {jj
> 0.345} {gg 0.6}
> {DW} inout {ft 0.13}  {fg 0.19}   {df 0.5}   {gh 0.2}   {hj 0.14}  {jj
> 0.53}  {gg 0.31}
> ....
>
> that will be converted into a table like below:
>
> --------------------------------------------------------------------------- ---------------------------------------------
>               |         Comp-1                    |
> Comp-2              | Comp-3
> Items      ------------------------------------
> |-------------------------    |---------------------------------
>               |   ft       fg     df           gh    |  ft      fg
> df   gh   | ft        fg       df     gh
> --------------------------------------------------
> |--------------------------------------------------------------
> ADA       |   0.23     0.9    0.45    0.33  |  0.3    0.4  0.1 0.2   |
> 0.22   0.44   0.66  0.8
> DW        |   0.13     0.19   0.5     0.2    |  0.1    0.2  0.5 0.6
> | 0.11   0.12   0.3   0.91
>
> ............................................................................ ................................................
> ....
> # more rows with data
>
> This table will first be created in an XML-like structure and will be
> displayed by the application as a table above.
>
> I see an obvious pattern for using multidimensional arrays but not
> sure how to do this row-by row to populate the XML-tree  ....
>
> For example, if I have the file comp-1 and the first row of the table
> {ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33}
>
> Do I have to read those all individual items into the array one by
> one?
>
> set row(comp1,ADA,ft) 0.23
> set row(comp1,DW,ft) 0.13
> ....
>
> and then write this into the XML file into appropriate tags?
>
> Please advise.
>
> Cheers!
> -Yil

If you are using tcl8.5 (or tcl 8.4 with the dict extension) ...

dict set row comp1 ADA ft 0.23
dict set row comp1 DW ft 0.12

The neat part is, $row ends up as a list that can be manipulated by
the list commands or the dict command, dumped to a file, looped over,
it's really quite nice.

-Sean "The Hypnotoad" Woods
From: yilmaz on
On Jun 12, 8:55 pm, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote:
> On Jun 12, 2:47 am, yilmaz <yyyyil...(a)gmail.com> wrote:
>
>
>
>
>
> > On Jun 12, 2:37 pm, yilmaz <yyyyil...(a)gmail.com> wrote:
>
> > > Hi, Please help me with the following problem. I have very little
> > > experience with multidimensional arrays and need your advice.
>
> > > I have several lists like below from files comp-1, comp-2, and comp-3:
>
> > > {ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33} {hj 0.11} {jj
> > > 0.345} {gg 0.6}
> > > {DW} inout {ft 0.13}  {fg 0.19}   {df 0.5}   {gh 0.2}   {hj 0..14}  {jj
> > > 0.53}  {gg 0.31}
> > > ....
>
> > > that will be converted into a table like below:
>
> > > --------------------------------------------------------------------------- ---------------------------------------------
> > >               |         Comp-1                    |
> > > Comp-2              | Comp-3
> > > Items      ------------------------------------
> > > |-------------------------    |---------------------------------
> > >               |   ft       fg     df           gh    |  ft      fg
> > > df   gh   | ft        fg       df     gh
> > > --------------------------------------------------
> > > |--------------------------------------------------------------
> > > ADA       |   0.23     0.9    0.45    0.33  |  0.3    0.4  0.1 0.2   |
> > > 0.22   0.44   0.66  0.8
> > > DW        |   0.13     0.19   0.5     0.2    |  0.1    0.2  0.5 0.6
> > > | 0.11   0.12   0.3   0.91
>
> > > ............................................................................ ................................................
> > > ....
> > > # more rows with data
>
> > > This table will first be created in an XML-like structure and will be
> > > displayed by the application as a table above.
>
> > > I see an obvious pattern for using multidimensional arrays but not
> > > sure how to do this row-by row to populate the XML-tree  ....
>
> > > For example, if I have the file comp-1 and the first row of the table
> > > {ADA} input {ft 0.23} {fg 0.9} {df 0.45} {gh 0.33}
>
> > > Do I have to read those all individual items into the array one by
> > > one?
>
> > > set row(comp1,ADA,ft) 0.23
> > > set row(comp1,DW,ft) 0.13
> > > ....
>
> > > and then write this into the XML file into appropriate tags?
>
> > > Please advise.
>
> > > Cheers!
> > > -Yil
>
> > A better example of the output table:
>
> >                   Comp-1            Comp-2                  Comp-3
> >          ft    fg     df    gh      ft     fg   df   gh     ft
> > fg     df    gh
> > ADA      0.23  0.9    0.45  0.33    0.3    0.4  0.1  0.2    0.22
> > 0.44   0.66  0.8
> > DW       0.13  0.19   0.5   0.2     0.1    0.2  0..5  0.6    0.11
> > 0.12   0.3   0.91
>
> > .....
> > .....
>
> If it is a multi-dimensional array which you want to access like one,
> use array keys as a list:
>
> % set Array([list Comp-1 ADA ft]) .23
>
> then to retrieve the data:
>
> % set Array([list Comp-1 ADA ft])
> .23
>
> Or loop through the entire set:
>
> % set X [list Comp-1 Comp-2 ...]
> % set Y [list ADA DW ...]
> % set Z [list ft fg df gh ...]
>
> % foreach x $X {
> %  foreach y $Y {
> %   foreach z $Z {
> %     set val [set Array([list $x $y $z])]
> % # do something with x, y, z and val
> % } } }
>
> This also works with sparse arrays

Thank you, Tom! Your second suggestion makes a lot of sense. I'll try
to implement this and let you know of the results. Thank you very
much!
 |  Next  |  Last
Pages: 1 2
Prev: open command
Next: foreach-loop for growing lists