Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tcl array problems

Status
Not open for further replies.

merang

Programmer
Aug 27, 2014
17
MY
Hi all..i have a problem regarding the use of array in tcl programming..i have a file like below:

**user johny
.subuser abu

basikal runner 2008 new
kereta honda 2005 second
motor yamaha 2010 new

.end abu

** edwind
.subuser ahmad

book himym 2007 second
house ara 2013 new
shoes nike 2014 new
sport chelsea 2006 legend

.end ahmad


i want to open this file and enter input, then the program will count how many element does the user have.
for example "enter user name : abu

output:
<user> <subuser> <number of items>

for your information, the "**" is the comment for the file that i want to open (johny is a comment)

how to use an array to get all the data from the file and get the above output using tcl?
thanks in advance
 
Hi

Please provide the exact expected output for the posted sample input, not just placeholders.

Personally I not get the logic - there is only subuser "abu", so wondering which items should be find for user "abu".

Anyway, not clear why would anybody use an array in this case. If only one user's data can be queried in one call, there is no need for any complex data type.


Feherke.
feherke.ga
 
Hi feherka..

Tq for your concern..actually my file is a netlist. The input file that I wrote is just an example on how the structure and how the netlist looks like..

The file looks like this

**Library name : etc1
**cellname : etc2

.subckt etc2

(List of instance)

.end etc2

**Library name : etc3
**cellname : etc4

.subckt etc4

(List of instance)

.end etc4

So I need to come out with a program using tcl that will ask for input
eg: please enter the library name : etc

Then the output should looks like this :

<libraryname> <cellname> <instance count>

The program need to count how many instance does certain library name or cellname have and will display on the output. I want to use an array to access the file.sp

Hope you can understand how the program works. :)
 
Hi

You still not posted the exact output for the sample input. So I assumed that maybe a library section may contain multiple subckt subsections and that is why you think to complex data structure. However that is still not a reason to use such thing :
Code:
[b]puts[/b] [teal]-[/teal]nonewline [i][COLOR=#009900]"please enter the library name : "[/color][/i]
[b]flush[/b] stdout
[b]gets[/b] stdin name

[b]set[/b] fil [COLOR=teal][[/color][b]open[/b] [i][COLOR=#009900]"netlist.txt"[/color][/i] r[teal]][/teal]

[b]set[/b] inside [COLOR=#993399]0[/color]
[b]set[/b] subckt [i][COLOR=#009900]""[/color][/i]
[b]while[/b] [teal]{[/teal] [COLOR=teal][[/color][b]gets[/b] [navy]$fil[/navy] line[teal]][/teal] [teal]>=[/teal] [COLOR=#993399]0[/color] [teal]}[/teal] [teal]{[/teal]
    [b]if[/b] [teal]{[/teal] [navy]$line[/navy] [teal]==[/teal] [i][COLOR=#009900]"**Library name : $name"[/color][/i] [teal]}[/teal] [teal]{[/teal]
        [b]set[/b] inside [COLOR=#993399]1[/color]
    [teal]}[/teal] [b]elseif[/b] [teal]{[/teal] [COLOR=teal][[/color] [b]string[/b] range [navy]$line[/navy] [COLOR=#993399]0[/color] [COLOR=#993399]16[/color] [teal]][/teal] [teal]==[/teal] [i][COLOR=#009900]"**Library name : "[/color][/i] [teal]}[/teal] [teal]{[/teal]
        [b]set[/b] inside [COLOR=#993399]0[/color]
    [teal]}[/teal]
    [b]if[/b] [teal]{[/teal] [navy]$inside[/navy] [teal]}[/teal] [teal]{[/teal]
        [b]if[/b] [teal]{[/teal] [COLOR=teal][[/color] [b]string[/b] range [navy]$line[/navy] [COLOR=#993399]0[/color] [COLOR=#993399]7[/color] [teal]][/teal] [teal]==[/teal] [i][COLOR=#009900]".subckt "[/color][/i] [teal]}[/teal] [teal]{[/teal]
            [b]set[/b] subckt [COLOR=teal][[/color] [b]string[/b] range [navy]$line[/navy] [COLOR=#993399]8[/color] end [teal]][/teal]
            [b]set[/b] item [COLOR=#993399]0[/color]
        [teal]}[/teal] [b]elseif[/b] [teal]{[/teal] [navy]$line[/navy] [teal]==[/teal] [i][COLOR=#009900]".end $subckt"[/color][/i] [teal]}[/teal] [teal]{[/teal]
            [b]puts[/b] [i][COLOR=#009900]"$name $subckt $item"[/color][/i]
            [b]set[/b] subckt [i][COLOR=#009900]""[/color][/i]
        [teal]}[/teal] [b]elseif[/b] [teal]{[/teal] [navy]$line[/navy] [teal]!=[/teal] [i][COLOR=#009900]""[/color][/i] [teal]}[/teal] [teal]{[/teal]
            [b]incr[/b] item
        [teal]}[/teal]
    [teal]}[/teal]
[teal]}[/teal]

[b]close[/b] [navy]$fil[/navy]


Feherke.
feherke.ga
 
For your info, in the netlist there are more than 30 different libraryname and cellname and also more than hundreds of instance. Can I use the code above for the netlist?
 
Hi

merang said:
Can I use the code above for the netlist?
I searched the web for "netlist" and "netlist file format" but found nothing that would look relevant. So I tested my code with a self-made text file composed from pieces from your earlier posts. Can not estimate whether you can use it as is, it needs modifications or is completely useless. Try it. As it does not alter the content of the input file, will not harm if fails.


Feherke.
feherke.ga
 
Thanks man..I will try it first then will update with you on how it works..thanks again..
 
Hi feherka,

The given code is not working when i try to do a long list of file. Maybe because of the string range that you have specified in your code. Is it possible to do without array as the netlist is a complex data structure. It consist of many library name, for each library name got cell name and under the cell name will have hundreds of instance. I really hope that you can help me on this..thanks.
 
Hi

merang said:
The given code is not working when i try to do a long list of file.
File length should not influence it in any way, as it reads it line by line. So regardless the file length, only one line is hold in the memory in any time.

No idea how secret can such netlist data be, but will really need something that precisely resembles the original structure.

The file I tested with ( comments added as explanations only in this post ) :
Code:
**Library name : etc1              [gray]# [navy]$line[/navy] [teal]==[/teal] [i][COLOR=#009900]"**Library name : $name"[/color][/i] is true, so library section starts here[/gray]
**cellname : etc2                  [gray]# \ ignored as not in subsection yet[/gray]
                                   [gray]# /[/gray]
.subckt etc2                       [gray]# [teal][[/teal] [b][black]string[/black][/b] [black]range[/black] [navy]$line[/navy] [COLOR=#993399]0[/color] [COLOR=#993399]7[/color] [teal]][/teal] [teal]==[/teal] [i][COLOR=#009900]".subckt "[/color][/i] is true, so subsection starts here[/gray]

basikal runner 2008 new            [gray]# \[/gray]
kereta honda 2005 second           [gray]#  > [navy]$line[/navy] [teal]!=[/teal] [i][COLOR=#009900]""[/color][/i] is true, so these get counted[/gray]
motor yamaha 2010 new              [gray]# /[/gray]

.end etc2                          [gray]# [navy]$line[/navy] [teal]==[/teal] [i][COLOR=#009900]".end $subckt"[/color][/i] is true, so subsection ends here and partial result is displayed[/gray]

#**Library name : etc3             [gray]# \ ( note the # at the line's start, so this not starts another section )[/gray]
**cellname : etc4                  [gray]# / ignored as not in subsection yet[/gray]

.subckt etc4                       [gray]# [teal][[/teal] [b][black]string[/black][/b] [black]range[/black] [navy]$line[/navy] [COLOR=#993399]0[/color] [COLOR=#993399]7[/color] [teal]][/teal] [teal]==[/teal] [i][COLOR=#009900]".subckt "[/color][/i] is true, so subsection starts here[/gray]

book himym 2007 second             [gray]# \[/gray]
house ara 2013 new                 [gray]#  \ [navy]$line[/navy] [teal]!=[/teal] [i][COLOR=#009900]""[/color][/i] is true, so these get counted[/gray]
shoes nike 2014 new                [gray]#  /[/gray]
sport chelsea 2006 legend          [gray]# /[/gray]

.end etc4                          [gray]# [navy]$line[/navy] [teal]==[/teal] [i][COLOR=#009900]".end $subckt"[/color][/i] is true, so subsection ends here and partial result is displayed[/gray]

So running it works like this :
Code:
[blue]master #[/blue] tclsh netlist.tcl 
please enter the library name : etc1
etc1 etc2 3
etc1 etc4 4

Please confirm whether the base theory of the script is correct and that is the expected output.


Feherke.
feherke.ga
 
hi feherka,

thanks for such a good explanation. the expected output is as you wrote but the etc4 should be under etc3 and not under etc1.should be like this:

master # tclsh netlist.tcl
please enter the library name : etc1
etc1 etc2 3

if run again the program:

please enter the library name : etc3
etc3 etc4 4


The problem i think is the string range. i think this program cannot use the string range as the list of commented lines is not just two. sorry i did not mentioned it before. below is an example of structure:

**Library name : etc1
**cellname : etc2
**this line is not fixed to 2 only
**can have more than 30 lines that starts with "**"
**the code should read either Library name and cellname only and will ignore other that starts with "**"

.subckt etc2

(List of instance)
basikal runner 2008 new etc2
kereta honda 2005 second etc2
motor yamaha 2010 new etc2
(can have up to hundreds of instance, the number is not fixed)
(the program will count on how many instance does certain library name and cellname have)

.end etc2

**Library name : etc3
**cellname : etc4
**this line is not fixed to 2 only
**can have more than 30 lines that starts with "**"
**the code should read either Library name and cellname only and will ignore other that starts with "**"


.subckt etc4

(List of instance)
book himym 2007 second etc4
house ara 2013 new etc4
shoes nike 2014 new etc4
sport chelsea 2006 legend etc4
(can have up to hundreds of instance, the number is not fixed)
(the program will count on how many instance does certain library name and cellname have)

.end etc4

first function is to display the instance count such as:

please enter the library name : etc1
etc1 etc2 3


the second function is to detect the instance is belong to which cellname, as the instance look like "sport chelsea 2006 legend etc4", i think we can use split command as it will split the lines and use lindex to take certain element such as:

set elem [split $file " "]
set instname [lindex $elem 0]
set cell [lindex $elem end]

so the second output should be like <sport> <etc4>

thanks in advance for your concern and help. hope this explanation helps :)





 
feherka's code seems to work for me. If we amend it slightly and run it on the file that you have shown in you last post, warts and all, then this code:

Code:
puts -nonewline "please enter the library name : "
flush stdout
gets stdin name

set fil [open "netlist.txt" r]

set inside 0
set subckt ""
while { [gets $fil line] >= 0 } {
    if { $line == "**Library name : $name" } {
        set inside 1
        continue
    } elseif { [ string range $line 0 16 ] == "**Library name : " } {
        set inside 0
    }
    if { $inside } {
        if { [ string range $line 0 7 ] == ".subckt " } {
            set subckt [ string range $line 8 end ]
            set item 0
            set count 1
        } elseif { $line == ".end $subckt" } {
            puts stdout "Total: $name $subckt $item"
            set subckt ""
        } elseif { $line != "" && [string range $line 0 1] != "**" } {
	    puts stdout "$count: [lindex $line 0] \<$subckt\>"
            incr item
            incr count
        }
    }
}

close $fil

produces this result:

Code:
please enter the library name : etc1
1: (List <etc2>
2: basikal <etc2>
3: kereta <etc2>
4: motor <etc2>
5: (can <etc2>
6: (the <etc2>
Total: etc1 etc2 6

please enter the library name : etc2

please enter the library name : etc3
1: (List <etc4>
2: book <etc4>
3: house <etc4>
4: shoes <etc4>
5: sport <etc4>
6: (can <etc4>
7: (the <etc4>
Total: etc3 etc4 7

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top