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

problem with arrays 1

Status
Not open for further replies.

baitazeare

Technical User
Apr 15, 2012
2
ES
Hello,

I'm new with tcl programming language, and I need your help:
I want to create an array with some conditions. I have a variable len that says me how long the arry must be, and on the other hand, I have in another variable bete the number that I must have in all positions in the array. For example:

%set len 5
%set bete 123

So this is what i want to achieve in an array:
array = 123 123 123 123 123

I don't know if it is clear...

Thanks in advance

looking forward to your help
 
Short Example
Code:
[COLOR=#804040][b]set[/b][/color] len [COLOR=#ff00ff]5[/color]
[COLOR=#804040][b]set[/b][/color] bete [COLOR=#ff00ff]123[/color]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Using Array:"[/color]
[COLOR=#0000ff]# create array[/color]
[COLOR=#804040][b]for[/b][/color] {[COLOR=#804040][b]set[/b][/color] i [COLOR=#ff00ff]1[/color]} {[COLOR=#008080]$i[/color] <= [COLOR=#008080]$len[/color]} {[COLOR=#804040][b]incr[/b][/color] i} {
  [COLOR=#804040][b]set[/b][/color] arr([COLOR=#008080]$i[/color]) [COLOR=#008080]$bete[/color]
}

[COLOR=#0000ff]# print array[/color]
[COLOR=#804040][b]for[/b][/color] {[COLOR=#804040][b]set[/b][/color] i [COLOR=#ff00ff]1[/color]} {[COLOR=#008080]$i[/color] <= [COLOR=#008080]$len[/color]} {[COLOR=#804040][b]incr[/b][/color] i} {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"arr([/color][COLOR=#008080]$i[/color][COLOR=#ff00ff]) = [/color][COLOR=#008080]$arr[/color][COLOR=#ff00ff]([/color][COLOR=#008080]$i[/color][COLOR=#ff00ff])"[/color]
}

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]Using List:"[/color]
[COLOR=#0000ff]# create list[/color]
[COLOR=#804040][b]set[/b][/color] lst {}
[COLOR=#804040][b]for[/b][/color] {[COLOR=#804040][b]set[/b][/color] i [COLOR=#ff00ff]1[/color]} {[COLOR=#008080]$i[/color] <= [COLOR=#008080]$len[/color]} {[COLOR=#804040][b]incr[/b][/color] i} {
  [COLOR=#804040][b]lappend[/b][/color] lst [COLOR=#008080]$bete[/color]
}

[COLOR=#0000ff]# print list[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]lst = [/color][COLOR=#6a5acd]\[[/color][COLOR=#ff00ff] [/color][COLOR=#008080]$lst[/color][COLOR=#ff00ff] [/color][COLOR=#6a5acd]\][/color][COLOR=#ff00ff]"[/color]

[COLOR=#804040][b]set[/b][/color] i [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]foreach[/b][/color] el [COLOR=#008080]$lst[/color] {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"lst([/color][COLOR=#008080]$i[/color][COLOR=#ff00ff]) = [/color][COLOR=#008080]$el[/color][COLOR=#ff00ff]"[/color]
  [COLOR=#804040][b]incr[/b][/color] i
}
Output:
Code:
Using Array:
arr(1) = 123
arr(2) = 123
arr(3) = 123
arr(4) = 123
arr(5) = 123

Using List:
$lst = [ 123 123 123 123 123 ]
lst(1) = 123
lst(2) = 123
lst(3) = 123
lst(4) = 123
lst(5) = 123
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top