LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2006, 11:41 AM   #1
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Rep: Reputation: 15
Wink 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
Many Thanks,

James
 
Old 05-28-2006, 03:39 PM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.
 
Old 05-28-2006, 03:49 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I think because llength is an operator which operates on sets, and not on arrays.

In your second example you assign a number of elements to a set. Hence llength returns the number of elements in the set.

On the first example you have an array. If you want to know the number of elements there, you should use array size arrayname

I take it [llength array2] is a typo?

jlinkels
 
Old 05-30-2006, 07:17 AM   #4
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
>> Im not sure what programming lanugae this

It's tcl. See the subject line of my first post

>> I think because llength is an operator which operates on sets, and not on arrays.

Yeah, I think you're onto something here. Frustratingly enough this is the error message I get:

Code:
can't read "array1": variable is array
Nice huh?
 
Old 05-30-2006, 07:42 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Code:
puts [array size array1]
or something similar should do.

See man n array
 
Old 05-31-2006, 03:35 PM   #6
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
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.

Am I correct?
 
Old 05-31-2006, 05:56 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Oh but you can extract the n-th item from a list equally well :-)

I don't know what the internal implementation of lists and arrays etc is.

Did you dowload and take the tcl tutor?

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.

jlinkels
 
Old 06-01-2006, 06:41 AM   #8
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
No, I didn't find that one. Thanks

I've been learning from (quite an old) book. I need to know a bit about it for work, but you're right it does look like a very powerful language.
 
Old 06-01-2006, 08:29 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
set counter 0

woops double posted

Last edited by bigearsbilly; 06-01-2006 at 08:42 AM.
 
Old 06-01-2006, 08:29 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 06-01-2006, 08:42 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
also it's inc counter
 
Old 06-02-2006, 05:59 AM   #12
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
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.

Thanks
 
Old 06-03-2006, 12:38 PM   #13
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Quote:
Originally Posted by jimieee
>> Im not sure what programming lanugae this

It's tcl. See the subject line of my first post
Ahh I dident know TCL was a language. I always thought it was just a library.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically set array length in Fortran77? halfpower Programming 2 12-15-2005 08:03 AM
finding array length from a reference (in Perl) lowpro2k3 Programming 1 06-22-2005 04:49 PM
what is devel, silc, tcl??? (newbie) chis Linux - Software 3 07-20-2004 08:27 AM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 01:31 AM
finding the length of an array in perl acid_kewpie Programming 5 09-25-2002 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration