INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

tcl array problems

tcl array problems

(OP)
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

RE: tcl array problems

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

RE: tcl array problems

(OP)
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. :)

RE: tcl array problems

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 --> Tcl

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
    } 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
        } elseif { $line == ".end $subckt" } {
            puts "$name $subckt $item"
            set subckt ""
        } elseif { $line != "" } {
            incr item
        }
    }
}

close $fil 

Feherke.
feherke.ga

RE: tcl array problems

(OP)
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?

RE: tcl array problems

Hi

Quote (merang)

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

RE: tcl array problems

(OP)
Thanks man..I will try it first then will update with you on how it works..thanks again..

RE: tcl array problems

(OP)
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.

RE: tcl array problems

Hi

Quote (merang)

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 --> netlist.txt

**Library name : etc1              # $line == "**Library name : $name" is true, so library section starts here
**cellname : etc2                  # \ ignored as not in subsection yet
                                   # /
.subckt etc2                       # [ string range $line 0 7 ] == ".subckt " is true, so subsection starts here

basikal runner 2008 new            # \
kereta honda 2005 second           #  > $line != "" is true, so these get counted
motor yamaha 2010 new              # /

.end etc2                          # $line == ".end $subckt" is true, so subsection ends here and partial result is displayed

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

.subckt etc4                       # [ string range $line 0 7 ] == ".subckt " is true, so subsection starts here

book himym 2007 second             # \
house ara 2013 new                 #  \ $line != "" is true, so these get counted
shoes nike 2014 new                #  /
sport chelsea 2006 legend          # /

.end etc4                          # $line == ".end $subckt" is true, so subsection ends here and partial result is displayed 

So running it works like this :

CODE --> command line

master # 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

RE: tcl array problems

(OP)
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 :)





RE: tcl array problems

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close