[Ro] started this page on July 8th, 2003
This parses Slashdot [http://www.slashdot.org] comments. It reads in the source of a comment page that is of type '''nested'''.
#!/usr/bin/tclsh
proc @ij {c_name i_j} {foreach {i j} $i_j break ; upvar $c_name c ; return [string range $c $i $j]}
proc =@ {c arr_name} {
upvar $arr_name v ; set n 0
while {[regexp -indices -start $n {
\s+?\s+(.+?) \(Score\:(.+?)\)\s+\s+
(.+?)\s+ |
\s+\s+(.+?)\s+ |
\s+\s+.+?\[(.+?)\]} $c MATCH i_cid i_title i_rating i_header i_body i_trailer]} {
#puts -nonewline . ; flush stdout
foreach var [info vars] {if {[regexp {i_(.+)} $var -> rvar]} {set $rvar [@ij c [set $var]]}}
lappend v(comments) $cid ; set v($cid,title) $title ; set v($cid,body) $body
if {![regexp {(.+)\, (.+)} $rating -> v($cid,mod_num) v($cid,mod_type)]} {set v($cid,mod_num) $rating ; set v($cid,mod_type) no}
if {![regexp {(\d+)">Parent} $trailer -> v($cid,parent)]} {set v($cid,parent) no}
if {![regexp {by (.+?) \((\d+)\) } [lindex [split $header \n] 0] -> v($cid,u_slash_home) v($cid,u_name) v($cid,u_id)]} \
{foreach el {u_slash_home u_name u_id} {set v($cid,$el) no}}
regexp {on (.+?) \(} [lindex [split $header \n] 1] -> v($cid,time)
set n [expr {[lindex $MATCH 1] + 1}]
foreach el [lsort [array names v $cid,*]] {puts [format {%20s %s} $el $v($el)]} ; puts --------------------- ; flush stdout
}
}
set f [open nested_comments.html] ; set c [read $f] ; close $f ; unset f
=@ $c v
puts done!
Enjoy!
|