ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172
Rep:
tcl newbie: can't count length of array
Hi,
Can someone tell me why I can't count the number of elements in an array initialised like this:
Code:
set counter 0
set input 0
while {$input != "."} {
gets stdin input
if {$input != "."} {
set array1($counter) $input
}
set counter [expr $counter + 1]
}
set array1_length [llength $array2]
puts "array1 has $array1_length records"
But I can when I initialise it like this?:
Code:
set working_array {1 2 3 4 5 6}
set working_array_count [llength $working_array]
puts $working_array_count
Im not sure what programming lanugae this is but in C i know that an array is a set length that when compiled the compiler will create memory allocation for the set size. Stdin is a buffer for imput from the keyboard and thus has a varible lengh. So when trying to compile the compiler doesent know how much memory to set for the array. so it gives you an error.
Mabye wrong if your doing perl or java as the code isent compiled.
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172
Original Poster
Rep:
Ah! There's something I wasn't quite aware of here:
"Tcl supports an object not found in most programming languages: the list"
It turns out that a list and an array are two different things as far as Tcl is concerned. llength is short for "list length", so it doesn't work on arrays.
From what I understood from here the main difference between a list and an array (in Tcl) is that whilst an array is an indexed set of values (it behaves similarly to a hash in Perl) a list is just a string that's divided up with seperators.
Lists provide a convienient, though not very efficient, way to store and manipulate data.
I found it very valuable, as I think tcl has an extremely steep learning curve. Just the kind of problem you struggled with... and tcl is full of them. Nevertheless, it is a great and powerful language.
well, anyway for a start you are getting the length of array1
and printing the length of array2 !!!!!
Code:
set counter 0
set input 0
while {$input != "."} {
gets stdin input
if {$input != "."} {
set array1($counter) $input
}
set counter [expr $counter + 1]
}
set array1_length [llength $array2]
puts "array1 has $array1_length records"
and it's array size array1
have you got tkcon?
it's good for playing about.
and tclhelp is good too
Last edited by bigearsbilly; 06-01-2006 at 08:34 AM.
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172
Original Poster
Rep:
Yeah, basically being a newbie I'm a bit of an ignoranus I realised that error after my first post and corrected it, but still had the problem with lists not being the same thing as arrays
Still these are good tips!
For reference I was just doing this so that I could practise make lists, merging them, sorting them, that sort of thing. This is what I ended up doing:
Code:
#!/usr/bin/tclsh
# Print the date
puts [exec date +%d/%m/%Y\ %H:M\n]
# Short explanation of what the program does
puts "This program takes two lists from standard input, joins them, sorts them and splits them again\n"
# Get the first list
puts "Please start entering values for the first list. Finish entering a . to finish: "
set input 0
set list1 { }
while {$input != "."} {
gets stdin input
if {$input != "."} {
lappend list1 $input
}
}
# Get the second list
puts "Please start entering values for the second list. Finish entering a . to finish: "
set input 0
set list2 { }
while {$input != "."} {
gets stdin input
if {$input != "."} {
lappend list2 $input
}
}
# Get the length of the lists
set list1_length [llength $list1]
set list2_length [llength $list2]
puts "list 1 has $list1_length records"
puts "list 2 has $list2_length records"
# Join the lists
set superlist [concat $list1 $list2]
# Print list
puts "The joined list contains:"
puts $superlist
# Sort the lists
set superlist [lsort -integer $superlist]
# Print the list
puts "This is the list after it's been sorted:"
puts $superlist
# Split the lists into their original lengths
# Blank the list
set list1 { }
set count 0
while {$count < $list1_length} {
lappend list1 [lindex $superlist $count]
set count [expr $count + 1]
}
# Blank the list
set list2 { }
while {$count < [expr $list1_length + $list2_length]} {
lappend list2 [lindex $superlist $count]
set count [expr $count + 1]
}
# Print both lists seperately
puts "The new contents of list 1:"
puts $list1
puts "The new contents of list 2:"
puts $list2
It's not quite perfect, but I'll have another go and add in the suggested improvements sometime over the weekend.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.