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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array size problem 1

Status
Not open for further replies.

stewang

Programmer
Aug 21, 2003
77
NZ
Code:
Hello all:
I confuse with the array size.

I got two codes for array size as follow;

codeOne

array set myarray {1 2 3 4}
puts "all the elements in the array are : [array get myarray]"
puts "the array size is : [array size myarray]" 

it suppose output 4, but the result is 2, why.

/////////////////////////////////////////


codeTwo

set x(1) one 
puts "The first one is :[array get x]"
set x(2.0) two 
puts "The second one is :[array get x] "
set x(three) 3
puts "The third two is : [array get x]"
set x("four") [list 1 2 3 4]
puts "The fourth one is : [array get x ]"

puts "the array size is : [array size x]"

IN here, it should output 8 for the arraysize, but it output 4. beacuse in the array has 8 elements, they are {"four"} {1 2 3 4} three 3 2.0 two 1 one  
why ??????

////////////////////////////////////////////////////
And I also confuse with the array size's difinition, whta does the folowing sentence means???
array size arrayName
    Returns a decimal string giving the number of elements in the array

rgds
stewang
 
You are confused.

A tcl array is a key and value pairing.
If you would like your first assignment to have
four elements you could have done:

Code:
foreach xx {1 2 3 4} {
           set array($x) $x
     }

Use parray instead array get. It is confusing you
obviously as to the value contents of your array.
 
Code:
Hi marsd:
What the x contains, is it a list, or just a varible.I tried to set a value to x ( set x 1),but it report errors

#set x 4
#array set x {1 2 3 4} 
foreach xx {1 2 3 4} {
           set array($x) $x
     }
puts "[array size xx]

rgds
stewang
 
Hi marsd:
Thanks for your help, I known why the result for the array size is different from normal way now.

rgds
stewang
 
As marsd said "A tcl array is a key and value pairing".
[array get] & [array set] deal with key-value pairs.

The following commands do the same: add two elements at an array.
Code:
  array set array1 {key1 value1 key2 value2}
  foreach {key value} {key1 value1 key2 value2}   { set array2($key) $value }
Be careful: array set don't reset the array, the elements are added.

[array get] returns all the pairs in a list.
[array names] returns only the keys in a list.
Code:
  array get array1
-> key1 value1 key2 value2
  array names array1
-> key1 key2
As you can see the first command returns twice the number of elements because it returns pairs.

Another hint: an array is not ordered so if you want the keys sorted you need use lsort:
Code:
  lsort [array names array1]
HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top