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

Quick list question 1

Status
Not open for further replies.

july07

Programmer
Aug 26, 2009
33
US
I have a list that looks like this:
{texta textb textc textd
text1a text1b text1c text1d
text2a text2b text2c
text3a text3b text3c text3d
text4a text4b text4c text4d text4e
text5a text5b text5c
text6a text6b text6c text6d text6e
text7a text7b text7c text7d text7e
text8a text8b text8c text8d text8e}

I want to be able to output each line by it self, because i need each list for different purpose. for example output "texta textb textc textd" and other lines seperately

I tried doing this:
Code:
set result {texta textb textc textd
text1a text1b text1c text1d
text2a text2b text2c 
text3a text3b text3c text3d
text4a text4b text4c text4d text4e
text5a text5b text5c 
text6a text6b text6c text6d text6e
text7a text7b text7c text7d text7e
text8a text8b text8c text8d text8e}

foreach output $result {
set i 0
puts [lindex $output $i]

incr i
}

But it wont come out right. I think i need a comma at the end of each line to output each line seperately, how do i do that?

Any help will be appreciated.


 
How do you recognize each separate line? In your example this would be obtained from the figure appearing in text??, but I assume this is only an example and that the content of the list may be any text.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Yes, that is only an example and the content of the list can be any text... It is just a list and im trying to output each line by it self.... Is it possible?

Thanks for your help
 
Hi

Not sure if I understand your goal, but take a look at this, may be closer to what you want. I highlighted the main difference :
Code:
[b]set[/b] i [purple]0[/purple]
[b]foreach[/b] output [highlight][teal][[/teal][b]split[/b][/highlight] [navy]$result[/navy] [highlight][green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal]][/teal][/highlight] [teal]{[/teal]
  [b]puts[/b] [green][i]"$i) $output"[/i][/green]
  [b]incr[/b] i
[teal]}[/teal]

Feherke.
 
Thanks for the post Feherke, not quite what i want...
What i want is to be able to output each line by itself.
For example if i want line 3, i want this "text2a text2b text2c" to be outputted and if i want line 9, i want this "text8a text8b text8c text8d text8e" to be outputted.

Thanks
 
Hi

Code:
[b]puts[/b] [teal][[/teal][b]lindex[/b] [teal][[/teal][b]split[/b] [navy]$result[/navy] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal]][/teal] [purple]2[/purple][teal]][/teal] [gray]# output line 3[/gray]
[b]puts[/b] [teal][[/teal][b]lindex[/b] [teal][[/teal][b]split[/b] [navy]$result[/navy] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal]][/teal] [purple]8[/purple][teal]][/teal] [gray]# output line 9[/gray]
If you will do this many times, better use only a single [tt]split[/tt] :
Code:
[b]set[/b] resultline [teal][[/teal][b]split[/b] [navy]$result[/navy] [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal]][/teal] 

[b]puts[/b] [teal][[/teal][b]lindex[/b] [navy]$resultline[/navy] [purple]2[/purple][teal]][/teal] [gray]# output line 3[/gray]
[b]puts[/b] [teal][[/teal][b]lindex[/b] [navy]$resultline[/navy] [purple]8[/purple][teal]][/teal] [gray]# output line 9[/gray]
Note that list index starts at 0.

Feherke.
 
Yes! that is exactly what i want.... Thanks Feherke!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top