This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

long line spliting...

Old posts that have not been replied to for several years.
Locked
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

long line spliting...

Post by GodOfSuicide »

i have a very long string (~600 chars) which i want to split up into some lines.

the line looks something like this :
<quite much html code></a><br><again much html code></a><br><even more html code> .... (and so on)</a><br> </td>
everytime there is the <br> (which means new line in html) tag i want the string to be split up so i can acces to each "real" html line directly.

i tried it with regsub & \n, but it didnt work.....

would be great if somebody could help.
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

set newline [split $oldline "<br>"]
will return a list with each line as a element in the list
Elen sila lúmenn' omentielvo
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Unfortunately split always splits on single letters. Splitting on "<br>" simply splits the line at <, b, r, and >.

I think GodOfSuicide was on the right track with regsub and \n.

Code: Select all

set html "<quite much html code></a><br><again much html code></a><br><even more html code> .... (and so on)</a><br> </td>"
regsub -all "<br>" $html "\n" html
set lines [split $html "\n"]
Note you don't get pure lines of text from this -- it leaves all other tags in the code. You could use another regsub to get rid of them.
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

I was not aware that it is only splitting on single chars... thanks for enlightening me stdragon :)
Elen sila lúmenn' omentielvo
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

Code: Select all

set html "<quite much html code></a><br><again much html code></a><br><even more html code> .... (and so on)</a><br> </td>"
regsub -all "<br>" $html "\n" html
set lines [split $html "\n"]
how could i acces to the lines with some kinda array (like in delphi information[1], information[2], etc) ? i could loop i throught a counter and check all lines easily with that.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Somthing like

Code: Select all

proc createarray {name input delimeter args} {
  set inject "\n"
  if {[lindex $args 0] != ""} { set inject "[lindex $args 0] }
  set output ""
  regsub -all -- $delimeter $input $inject output
  set i 1
  foreach a [split $output $inject] {
    uplevel 1 [list set "${name}(${i})" $a]
  }
}
You can use it like follows.

Code: Select all

set html "<quite much html code></a><br><again much html code></a><br><even more html code> .... (and so on)</a><br> </td>"

createarray this $html "<br>"
puts stdout "First line: $this(1)"
puts stdout "Second: $this(2)"
Unlike some langs, you can't go out of bounds with Tcl arrays. It causes an error of "Variable doesn't exist".

You may find it is far easier to stick with the existing code (the one posted in your previous reply), and use "[lindex $lines 0]" and so on.
Locked