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.

Arrays and differences

Old posts that have not been replied to for several years.
Locked
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Arrays and differences

Post by GodOfSuicide »

I'm currently rewriting all of my newstickers

until now i used some kinda code like this (written just to show what i mean):

Code: Select all

if {$site_data(0) != $site_newdata(0)} {
putserv "PRIVMSG #chan :$site_newdata(0)" 
set site_data(0) $site_newdata(0)
}
Now i would like to do it another way :
if there have been x new entrys on the page the array shifts down by x entrys
but : how do i check for this data and make a loop for the output ?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: Arrays and differences

Post by user »

GodOfSuicide wrote:if there have been x new entrys on the page the array shifts down by x entrys
but : how do i check for this data and make a loop for the output ?
What do you mean by "the array SHIFTS down by x entrys"?

If you store the last post and have a list of current posts, just check how many elements are before/after (depending on the order) the element matching the 'last post' value.
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

there is a maximum of y fields in the array (if the page only links the latest y news, etc)

on z new posts the post that was first (0) befor is now on 0+z position, and the last z ones are now gone
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

This would require two small function to perform this. One to push the array, and another to trim it again.

First note: Tcl claims these are arrays. This is not true, they are actualy "Hash tables".

The code below will push an ellement onto the start of the array.

Code: Select all

proc array_push {name data} {
  array set temp [list]
  upvar 1 $name this
  foreach idx [array names this] {
    set temp([expr $idx + 1]) $this($idx)
  }
  set temp(0) $data
  array set this [array get temp]
}
Use it like this

Code: Select all

set array(0) "test value"
set array(1) "another value"
set array(2) "More data"
array_push array "some more data again"
The code below, will trim the array down to X elements

Code: Select all

proc array_trim {name X} {
  upvar 1 $name this
  set list [lsort -integer [array names this]]
  set limit [expr [llength $list] - ( $X + 1 )]
  if {$limit < 0} { return }
  foreach a [lrange $list "end-${limit}" end] {
    array unset this $a
  }
}
Test it with the following

Code: Select all

set array(0) "test value"
set array(1) "another value"
set array(2) "More data"
set array(3) "More data 123"
set array(4) "More data 456"
set array(5) "More data 789"
set array(6) "More data 741"
set array(7) "More data 852"
array_trim array 5
Here is the results of the tests above, using this small snippet.

Code: Select all

foreach {a b} [array get array] {
  puts stdout "${a}: ${b}"
}
array_push
0: some more data again
1: test value
2: another value
3: More data
array_trim
4: More data 456
0: test value
1: another value
2: More data
3: More data 123
Note: The order of output is not important. As ntoed, they are hash lists, and will never be in order. To get ordered info, use a sorted foreach loop

Code: Select all

foreach array_idx [lsort -integer [array names array_name]] {
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

i was meaning an other thing

The old parsed data (lets say 4 arrayelements) is compared to the new parsed data (also 4 elements).

old :
news 123 (url : 123)
news 234 (url : 234)
news 345 (url : 345)
news 456 (url : 456)
new :
news abc (url : abc)
news bcd (url : bcd)
news 123 (url : 123)
news 234 (url : 234)
right now there were 2 new data sets in NEW that haven't been in OLD befor. Now i want to output these 2 new entrys and then write the content of NEW -> OLD so i can check again the next time if there have been new sets.
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

seams noone has an idea what i am talking about :(
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

GodOfSuicide wrote:seams noone has an idea what i am talking about :(
I think I do. Tcl's "arrays" are not suited for this kind of task. Use a list.

To add one element to the start of a list and keep the number of elements below a given value, use something like this:

Code: Select all

set list [concat [list $new] [lrange $list 0 2]]
or

Code: Select all

set list [lrange [concat [list $new] $list] 0 3]
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

hm... maybe you have some function for me, too ;)

i've got the following arrays

Code: Select all

array(chan1,1,var1) "var1"
array(chan1,1,var2) "var2"
array(chan1,2,var1) "var3"
array(chan1,2,var2) "var4"
array(chan1,3,var1) "var5"
array(chan1,3,var2) "var6"
...
and so on.

i unset all arrays chan1,2 using

Code: Select all

array unset array chan1,2,*
now i'd need a proc that "refills" the array. means, array(chan1,3,var1)
should become array(chan1,2,var1), array(chan1,4,var1) should become
array(chan1,3,var1). hope you see my point.

guess i'd just have to change some code in the array_push proc of ppslim,
but, however, i don't know what it does exactly.
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
Locked