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.

splitting strings

Old posts that have not been replied to for several years.
Locked
s
sirkrunch
Voice
Posts: 19
Joined: Wed Oct 09, 2002 2:36 am

splitting strings

Post by sirkrunch »

I have this
$string /path/to/a/dir/and/file.txt

i want to split it, have one variable with the full path but no file
and another with just the fille name, how do i go about that??
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

i'm not that good in tcl, but i can tell you what logical way you have to go:

cos in the filename ther cant be a "/" character you have to find the position of the last "/" in the string.

Then copy all from first character in the string to the position you just found into a variable. Do the same from the position you found + 1 to the last character. Then you have split it up.

/path/to/a/dir/and/file.txt
______________|
find this one

/path/to/a/dir/and/file.txt
|---------------------|
copy from start till last "/"

/path/to/a/dir/and/file.txt
_______________|------|
copy from last "/" + 1 to end.

now somebody who knows TCL got to write the code, but i hope my theoretical method can help.

(board unformated my " "'s, so i just made "_"'s)
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

set line "/path/to/a/dir/and/file.txt"
set result [string trim $line "file.txt"]

result: /path/to/a/dir/and/
Once the game is over, the king and the pawn go back in the same box.
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

or:

Code: Select all

set line "/path/to/a/dir/and/file.txt" 
set dir [lindex [split $line /] end]  #will return "file.txt"
set file [join [lrange [split $line /] 0 end-1] /]  #will return "/path/to/a/dir/and"
this will give you both dir and filename, even if you don't know the filename

btw.. love your "graphical" demonstration GodOfSuicide :D
Elen sila lúmenn' omentielvo
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Tcl has a lovely set of built in commands, for operating on filenames.

What makes it even better, it works on ALL OS's. Meanign there are nor incompatabilities between scripts.

Code: Select all

set fullfile "/my/file/is/here/and/called/ppslim.doc"
set dirname [file dirname $fullfile]
set filename [file tail $fullfile]
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Haha, now you have 3 types to choose from :)
Once the game is over, the king and the pawn go back in the same box.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Make it four!

set fullfile "/path/to/file.ext"
regexp {(^.*)/([^/]*$)} $fullfile match dirname filename

ppslim's way is best though
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

Papillon wrote: btw.. love your "graphical" demonstration GodOfSuicide :D
that was the way i do it in delphi....so i tried to symbolize my way :D
Locked