Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A newbie bug, but where?

36 views
Skip to first unread message

Richard Owlett

unread,
Sep 15, 2018, 12:44:44 PM9/15/18
to
My test code is:

set infile "bookmarks.html"
set fd_in [open $infile r]

set line 0
set mylist [list "<DT><H3" "<DT><A"]

;# <DT><H3 introduces a HEADER
;# <DT><A introduces a URL with associated Title

while { ! [eof $fd_in] && ($line <= 25) } {
incr line
gets $fd_in b_line
set a_line [string trimleft $b_line]
set x [lsearch $mylist $a_line ]
puts "$x [string range $a_line 0 20]"
}

close $fd_in

Selected output is:

-1 <!DOCTYPE NETSCAPE-Bo
-1 <!-- This is an autom
-1 <DT><A HREF="http://1 <=== expected 1 <DT><A HREF="http://1
-1 <DT><A HREF="file:/// <=== expected 1 <DT><A HREF="file:///
-1 <DT><H3 ADD_DATE="133 <=== expected 0 <DT><H3 ADD_DATE="133
-1 <DL><p>

Is the problem with
set mylist [list "<DT><H3" "<DT><A"]
or with
set x [lsearch $mylist $a_line ]
?

TIA

Robert Heller

unread,
Sep 15, 2018, 1:31:00 PM9/15/18
to
At Sat, 15 Sep 2018 11:44:36 -0500 Richard Owlett <row...@cloud85.net> wrote:

>
> My test code is:
>
> set infile "bookmarks.html"
> set fd_in [open $infile r]
>
> set line 0
> set mylist [list "<DT><H3" "<DT><A"]
>
> ;# <DT><H3 introduces a HEADER
> ;# <DT><A introduces a URL with associated Title
>
> while { ! [eof $fd_in] && ($line <= 25) } {
> incr line
> gets $fd_in b_line
> set a_line [string trimleft $b_line]
> set x [lsearch $mylist $a_line ]

Replace this with:

set sp1 [string first " " $a_line]
set key [string range $a_line 0 [expr {$sp1 - 1}]]
set x [lsearch $mylist $key]

You are trying to match *all* of a_line in your list, when what you want are
the two command prefixes: "<DT><H3" and "<DT><A". Note the above code
*assumes* that the <H3> tag has one or more tag options (eg it is something
like <H3 class=...> or <H3 style=...>, etc.). This will fail if it is a bare
"<H3>".

If things are not as simple minded, you might do better with something like
this code:

while { ! [eof $fd_in] && ($line <= 25) } {
incr line
gets $fd_in b_line
set a_line [string trimleft $b_line]
set x -1
foreach p $mylist {
if {[regexp -nocase "^$p" $a_line] > 0} {
set x [lsearch -exact $mylist $p]
break;
}
}
puts "$x [string range $a_line 0 20]"
}

> puts "$x [string range $a_line 0 20]"
> }
>
> close $fd_in
>
> Selected output is:
>
> -1 <!DOCTYPE NETSCAPE-Bo
> -1 <!-- This is an autom
> -1 <DT><A HREF="http://1 <=== expected 1 <DT><A HREF="http://1
> -1 <DT><A HREF="file:/// <=== expected 1 <DT><A HREF="file:///
> -1 <DT><H3 ADD_DATE="133 <=== expected 0 <DT><H3 ADD_DATE="133
> -1 <DL><p>
>
> Is the problem with
> set mylist [list "<DT><H3" "<DT><A"]
> or with
> set x [lsearch $mylist $a_line ]
> ?
>
> TIA
>
>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

Richard Owlett

unread,
Sep 15, 2018, 3:07:24 PM9/15/18
to
On 09/15/2018 12:30 PM, Robert Heller wrote:
> At Sat, 15 Sep 2018 11:44:36 -0500 Richard Owlett <row...@cloud85.net> wrote:
>
>>
>> My test code is:
>>
>> set infile "bookmarks.html"
>> set fd_in [open $infile r]
>>
>> set line 0
>> set mylist [list "<DT><H3" "<DT><A"]
>>
>> ;# <DT><H3 introduces a HEADER
>> ;# <DT><A introduces a URL with associated Title
>>
>> while { ! [eof $fd_in] && ($line <= 25) } {
>> incr line
>> gets $fd_in b_line
>> set a_line [string trimleft $b_line]
>> set x [lsearch $mylist $a_line ]
>
> Replace this with:
>
> set sp1 [string first " " $a_line]
> set key [string range $a_line 0 [expr {$sp1 - 1}]]
> set x [lsearch $mylist $key]
>
> You are trying to match *all* of a_line in your list,

NO NO <laughter &/or groan>.
My mental model was basing comparisons on BASIC's INSTR$ command.
I'll have to spend some time looking at what you said below.

There is a "GOTCHA" here somewhere.
Finding it will be, so to speak, educational.
Especially as I thought I was copying *VERBATIM* working code -- only
difference I could see was VALUE of some variables.

> when what you want are
> the two command prefixes: "<DT><H3" and "<DT><A". Note the above code
> *assumes* that the <H3> tag has one or more tag options (eg it is something
> like <H3 class=...> or <H3 style=...>, etc.). This will fail if it is a bare
> "<H3>".

I thought I was looking at the first (unspecified #) characters of a
string. Once again think of BASIC's INSTR command.,

Robert Heller

unread,
Sep 15, 2018, 4:03:21 PM9/15/18
to
Tcl != BASIC
0 new messages