Home > Archive > Tcl > March 2004 > Newbie question.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Piotr Borowski 2004-03-30, 5:47 am |
| Hello,
I wanted to write a simple script that would parse data from a to b.
a.
-TypDok-Wersja-PIMW-
5 6 -1
b.
TypDok;Wersja;PIMW;
5;6;-1;
I cut off first characters of each line:
[string trim "-"]
[string trim " "]
And then used split and join functions to get requested list.
set posplicie [split $poobcieciu "-"]
set podolaczeniu [join $posplicie ";"]
Eventually I got empty list elements. And my parsing looks like:
TypDok;Wersja;PIMW;
5;;;;;;6;;;;;;-1;
Is there any way to get rid of that empty elements?
Regards,
Piotr Borowski
| |
| Bruce Hartweg 2004-03-30, 9:50 am |
|
Piotr Borowski wrote:
> Hello,
>
> I wanted to write a simple script that would parse data from a to b.
>
> a.
> -TypDok-Wersja-PIMW-
> 5 6 -1
> b.
> TypDok;Wersja;PIMW;
> 5;6;-1;
>
> I cut off first characters of each line:
>
> [string trim "-"]
> [string trim " "]
>
> And then used split and join functions to get requested list.
>
> set posplicie [split $poobcieciu "-"]
> set podolaczeniu [join $posplicie ";"]
>
>
> Eventually I got empty list elements. And my parsing looks like:
>
> TypDok;Wersja;PIMW;
> 5;;;;;;6;;;;;;-1;
>
> Is there any way to get rid of that empty elements?
>
It is my guess that your code also does a split with whitespace.
split works on *every* character, so multiple whitespace creates
empty elements. try using splitx from tcllib, or (and ONLY if
you really know your data) if it is always a set of whitepsace
seperated numbers (no characters like { [ " ] } ) then you
could skip the split entirely adn just do the join.
Bruce
| |
| Bryan Schofield 2004-03-30, 7:37 pm |
| Piotr Borowski wrote:
> Hello,
>
> I wanted to write a simple script that would parse data from a to b.
>
> a.
> -TypDok-Wersja-PIMW-
> 5 6 -1
> b.
> TypDok;Wersja;PIMW;
> 5;6;-1;
>
> I cut off first characters of each line:
>
> [string trim "-"]
> [string trim " "]
>
> And then used split and join functions to get requested list.
>
> set posplicie [split $poobcieciu "-"]
> set podolaczeniu [join $posplicie ";"]
>
>
> Eventually I got empty list elements. And my parsing looks like:
>
> TypDok;Wersja;PIMW;
> 5;;;;;;6;;;;;;-1;
>
> Is there any way to get rid of that empty elements?
>
> Regards,
>
> Piotr Borowski
>
>
>
Here's a quick console hack for processing lines of type "a" into lines
of type "b".
% proc convert {line} {
# determine if the line is -TypDok-Wersja-PIMW- format
if {[string index $line 0] == "-"} {
# remove the leading "-" and then
# replace all remaining "-" with ";"
return [regsub -all -- {-} [string range $line 1 end] ";"]
} else {
# trim any leading or trailing white space, add a terminating
# ";" then replace all consecutive sequences of whitespace
# with ";"
return [regsub -all -- {\s+} "[string trim $line];" ";"]
}
}
% convert -TypDok-Wersja-PIMW-
TypDok;Wersja;PIMW;
% convert "5 6 -1"
5;6;-1;
Hope that helps.
-- bryan
|
|
|
|
|