egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

list specific chapter from a href tag?
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    egghelp.org community Forum Index -> Archive
View previous topic :: View next topic  
Author Message
Jagg
Halfop


Joined: 24 Jan 2004
Posts: 53

PostPosted: Sat Jan 24, 2004 11:43 am    Post subject: list specific chapter from a href tag? Reply with quote

Hi,

I have a html-file. In the source code there are many a href-tags which looks like

<a href = "http://www.url.com/e?t=WORD" target="_blank">WORD</a>

<a href = "http://www.url.com/e?t=WORD2" target="_blank">WORD2</a>

and so on....

How can I list all blue WORD* ?

Thanks
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Sat Jan 24, 2004 9:34 pm    Post subject: Re: list specific chapter from a href tag? Reply with quote

Try this:
Code:
foreach {word word} [regexp -all -inline {"http://www.url.com/e\?t=([^"]+)"} $html] {
  lappend words $word
}

_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
Jagg
Halfop


Joined: 24 Jan 2004
Posts: 53

PostPosted: Sun Jan 25, 2004 1:19 pm    Post subject: Re: list specific chapter from a href tag? Reply with quote

THANKS A LOT!!!!!

I get now a list from all WORD like:
Quote:
WORD WORD2 WORD3 and so on...


How can I write a comma between these words?
Back to top
View user's profile Send private message
strikelight
Owner


Joined: 07 Oct 2002
Posts: 708

PostPosted: Sun Jan 25, 2004 5:35 pm    Post subject: Reply with quote

By converting it to a string with "join" and telling it what to join with, ie. a comma:

Code:

set text [join $yourlist ", "]
Back to top
View user's profile Send private message Visit poster's website
Jagg
Halfop


Joined: 24 Jan 2004
Posts: 53

PostPosted: Wed Jan 28, 2004 9:17 am    Post subject: Reply with quote

Ok, it works perfect now..... BUT there is a problem Confused

The server allows only ~445 characters per line! Sometimes the output of the above script is more than 445 characters so it stops after this amount of ch. Sad

With which command I can say/split the output in two, three lines when var $a is more than 445 characters?
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Wed Jan 28, 2004 2:30 pm    Post subject: Reply with quote

Jagg wrote:
With which command I can say/split the output in two, three lines when var $a is more than 445 characters?

There's no single command to do that Razz

This is a pretty straight forward word wrapper. the optional third value can be used to split on a different char than space (only one char..sorry Razz) It returns a list of "lines" that you can do whatever you like with Smile
Code:

proc wordwrap {str {len 70} {splitChr { }}} {
   set out [set cur {}]; set i 0
   foreach word [split [set str][unset str] $splitChr] {
      if {[incr i [string len $word]]>$len} {
         lappend out [join $cur $splitChr]
         set cur [list $word]
         set i [string len $word]
      } {
         lappend cur $word
      }
      incr i
   }
   lappend out [join $cur $splitChr]
}

ps: it won't split words even if they're longer than the max length. feature or bug? you decide Razz
pps: it will return a list with one empty element if you feed it an empty string
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
strikelight
Owner


Joined: 07 Oct 2002
Posts: 708

PostPosted: Wed Jan 28, 2004 7:42 pm    Post subject: Reply with quote

user wrote:
Jagg wrote:
With which command I can say/split the output in two, three lines when var $a is more than 445 characters?

There's no single command to do that Razz

This is a pretty straight forward word wrapper. the optional third value can be used to split on a different char than space (only one char..sorry Razz) It returns a list of "lines" that you can do whatever you like with Smile
Code:

proc wordwrap {str {len 70} {splitChr { }}} {
   set out [set cur {}]; set i 0
   foreach word [split [set str][unset str] $splitChr] {
      if {[incr i [string len $word]]>$len} {
         lappend out [join $cur $splitChr]
         set cur [list $word]
         set i [string len $word]
      } {
         lappend cur $word
      }
      incr i
   }
   lappend out [join $cur $splitChr]
}

ps: it won't split words even if they're longer than the max length. feature or bug? you decide Razz
pps: it will return a list with one empty element if you feed it an empty string


What's the point of unsetting the local variable str? Sure you free up a little bit of memory, but with the command substitution you are adding cpu time... The local variable will be destroyed once the procedure has exited anyways, so for the little time it does spend inside the proc, plus the slight cpu overhead, there's no advantage to unsetting it.
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Wed Jan 28, 2004 7:55 pm    Post subject: Reply with quote

strikelight wrote:
What's the point of unsetting the local variable str? Sure you free up a little bit of memory, but with the command substitution you are adding cpu time... The local variable will be destroyed once the procedure has exited anyways, so for the little time it does spend inside the proc, plus the slight cpu overhead, there's no advantage to unsetting it.

The point is to not have two copies of the entire input stored in memory at any point. In my opinion, saving ~50% memory (for large amounts of data) outweights the disadvantage of the tiny cpu usage added by the unset.

But it probably doesn't matter for what these folks will use it for anyway...and to be perfectly honest I did it to make the boring code have a slightly interesting part for us to talk about right now Razz
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
strikelight
Owner


Joined: 07 Oct 2002
Posts: 708

PostPosted: Wed Jan 28, 2004 8:12 pm    Post subject: Reply with quote

user wrote:
strikelight wrote:
What's the point of unsetting the local variable str? Sure you free up a little bit of memory, but with the command substitution you are adding cpu time... The local variable will be destroyed once the procedure has exited anyways, so for the little time it does spend inside the proc, plus the slight cpu overhead, there's no advantage to unsetting it.

The point is to not have two copies of the entire input stored in memory at any point. In my opinion, saving ~50% memory (for large amounts of data) outweights the disadvantage of the tiny cpu usage added by the unset.

But it probably doesn't matter for what these folks will use it for anyway...and to be perfectly honest I did it to make the boring code have a slightly interesting part for us to talk about right now Razz


Nothing wrong with us having a discussion about it though Wink

Code:

% proc a {mylist} {set i 0; foreach word $mylist {incr i}}
% proc b {mylist} {set i 0; foreach word [set mylist][unset mylist] {incr i}}
% time {a $mylist} 1000
26 microseconds per iteration
% time {b $mylist} 1000
81 microseconds per iteration
% time {a $mylist} 1000
36 microseconds per iteration
% time {b $mylist} 1000
91 microseconds per iteration


so while you are correct in stating that you save ~50% in memory,
you are also adding ~200% to ~300% in cpu time Wink
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Wed Jan 28, 2004 8:27 pm    Post subject: Reply with quote

strikelight wrote:
Code:

% proc a {mylist} {set i 0; foreach word $mylist {incr i}}
% proc b {mylist} {set i 0; foreach word [set mylist][unset mylist] {incr i}


Your test is not fair, as 'unset' returns an empty STRING, making the list before it become a string that has to be translated back to a list by foreach. (oh, the wonderfull internals of tcl ;P)
Try this instead and you'll see the unset adds very little to the total cpu usage Smile
Code:

proc a it {set i 0; foreach word [split $it] {incr i}; set i}
proc b it {set i 0; foreach word [split [set it][unset it]] {incr i}; set i}

_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
strikelight
Owner


Joined: 07 Oct 2002
Posts: 708

PostPosted: Wed Jan 28, 2004 8:36 pm    Post subject: Reply with quote

user wrote:
strikelight wrote:
Code:

% proc a {mylist} {set i 0; foreach word $mylist {incr i}}
% proc b {mylist} {set i 0; foreach word [set mylist][unset mylist] {incr i}


Your test is not fair, as 'unset' returns an empty STRING, making the list before it become a string that has to be translated back to a list by foreach
try this instead and you'll see the unset adds very little to the total cpu usage Smile
Code:

proc a it {set i 0; foreach word [split $it] {incr i}; set i}
proc b it {set i 0; foreach word [split [set it][unset it]] {incr i}; set i}


You are correct...

Code:

% proc b {mylist} {set i 0; foreach word [split [set mylist][unset mylist]] {incr i}}
% proc a {mylist} {set i 0; foreach word [split $mylist] {incr i}}
% time {a $mylist} 1000
126 microseconds per iteration
% time {b $mylist} 1000
178 microseconds per iteration


Still ~50% cpu increase though.
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Wed Jan 28, 2004 8:38 pm    Post subject: Reply with quote

How long is the string you're testing it with? To be on-topic it should be > 445 bytes Razz
Oh..and what's the point of 'i'?
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
strikelight
Owner


Joined: 07 Oct 2002
Posts: 708

PostPosted: Wed Jan 28, 2004 8:48 pm    Post subject: Reply with quote

user wrote:
How long is the string you're testing it with? To be on-topic it should be > 445 bytes Razz
Oh..and what's the point of 'i'?


It was 271 bytes....
Now it is:
Code:

% string bytelength $mylist
547


'i' just gives the procs something to do to simulate that they are doing something meaningful ;x

Code:

% time {a $mylist} 1000
305 microseconds per iteration
% time {b $mylist} 1000
391 microseconds per iteration
% time {a $mylist} 1000
277 microseconds per iteration
% time {b $mylist} 1000
336 microseconds per iteration


So it's STILL ~20% - ~30% increase.

heh. I guess we can agree, like most things, it's a trade off of memory vs performance.
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Thu Jan 29, 2004 6:35 am    Post subject: Reply with quote

strikelight wrote:
heh. I guess we can agree, like most things, it's a trade off of memory vs performance.

Yeah...it seems most tcl versions are faster at setting the value to an empty string instead of deleting the entire variable, so I guess I should make it
Code:
foreach word [split [set str][set str ""] $splitChr] {...}
Smile
And to make it even faster I could replace all variable substitutions with [set ..], but then the code will be much harder to read.
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
strikelight
Owner


Joined: 07 Oct 2002
Posts: 708

PostPosted: Thu Jan 29, 2004 10:20 pm    Post subject: Reply with quote

user wrote:
strikelight wrote:
heh. I guess we can agree, like most things, it's a trade off of memory vs performance.

Yeah...it seems most tcl versions are faster at setting the value to an empty string instead of deleting the entire variable, so I guess I should make it
Code:
foreach word [split [set str][set str ""] $splitChr] {...}
Smile
And to make it even faster I could replace all variable substitutions with [set ..], but then the code will be much harder to read.


From my tests, variable substitutions seem to be on exact par with command substitutions. (except for those command substitutions which also involve variable substitutions at the same time, of course).
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    egghelp.org community Forum Index -> Archive All times are GMT - 4 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber