Monitoring a list?

Monitoring a list?

am 04.09.2007 16:21:11 von jis

i have a list which update with time.

A - 10
B - 20
A - 10
C - 300

i want to find which one is changing current. For eg if B is running.
It decrements till it reaches 0. I want to catch B is running. All
variables go back to zero and then disappears fromt he list.

I have very lengthy solutions. Can anybody suggest a clever solution
for this

Now I check the list every 5 seconds.

Cheers,
jis

Re: Monitoring a list?

am 04.09.2007 16:47:44 von jurgenex

jis wrote:
> i have a list which update with time.
>
> A - 10
> B - 20
> A - 10
> C - 300
>
> i want to find which one is changing current. For eg if B is running.
> It decrements till it reaches 0. I want to catch B is running. All
> variables go back to zero and then disappears fromt he list.

In Perl a list is a value. It has a fixed length and its members do not
change, e.g. ('foo', 'bar', 1, 2, 3, 'fuzz', $var)
is a list with 7 elements where the last element is the _current_ value of
$var at the time of evaluation.

Obviously you must be talking about something different, but what?

jue

Re: Monitoring a list?

am 04.09.2007 16:59:04 von Ben Morrow

Quoth jis :
> i have a list which update with time.
>
> A - 10
> B - 20
> A - 10
> C - 300
>
> i want to find which one is changing current. For eg if B is running.
> It decrements till it reaches 0. I want to catch B is running. All
> variables go back to zero and then disappears fromt he list.
>
> I have very lengthy solutions. Can anybody suggest a clever solution
> for this

Since you have provided no code, nor any indication of how you get hold
of this list, I can't provide much help. Have you seen the Posting
Guidelines?

However, in simple terms you need to get the list into some data
structure, say

my $list = [
['A', 10],
['B', 20],
['A', 10],
['C', 300],
];

(if you don't understand this, read perldoc perllol and perldoc perldsc)
and then use something similar to (completely untested)

my $list = check_the_list();
my $lastlist;

while (1) {
$lastlist = $list;
$list = check_the_list() or last;

my @gone;
for (0..$#{$list}) {
# if this entry is still there, move onto the next
$list->[$_][0] eq $lastlist->[$_][0] and next;

# remove this entry from $lastlist and try again
push @gone, $lastlist->[$_][0];
splice @$lastlist, $_, 1;
redo;
}

my @changed =
# we want the names...
map { $list->[$_][0] }
# ...of the entries that have changed
grep { $list->[$_][1] != $lastlist->[$_][1] }
0..$#{$list};

# do something with @changed and @gone

sleep 5;
}

I used arrays above as your keys above don't seem to be unique... if
they *are* unique, or if you can make them so, using a hash would be
better, as the above is rather ugly (using array indices is always
ugly), and make several important assumptions:

The order of entries in the list will never change.

The list never acquires any new entries.

An entry will never replace another with the same name; that is, the
list will never go from

B 1
A 2
A 3
C 4

to

B 1
A 2
C 4

as you now cannot tell which 'A' it is has vanished.

> Now I check the list every 5 seconds.

And what difference does that make to anything?

Ben

Re: Monitoring a list?

am 04.09.2007 17:34:34 von jis

On Sep 4, 9:59 am, Ben Morrow wrote:
> Quoth jis :
>
> > i have a list which update with time.
>
> > A - 10
> > B - 20
> > A - 10
> > C - 300
>
> > i want to find which one is changing current. For eg if B is running.
> > It decrements till it reaches 0. I want to catch B is running. All
> > variables go back to zero and then disappears fromt he list.
>
> > I have very lengthy solutions. Can anybody suggest a clever solution
> > for this
>
> Since you have provided no code, nor any indication of how you get hold
> of this list, I can't provide much help. Have you seen the Posting
> Guidelines?
>
> However, in simple terms you need to get the list into some data
> structure, say
>
> my $list = [
> ['A', 10],
> ['B', 20],
> ['A', 10],
> ['C', 300],
> ];
>
> (if you don't understand this, read perldoc perllol and perldoc perldsc)
> and then use something similar to (completely untested)
>
> my $list = check_the_list();
> my $lastlist;
>
> while (1) {
> $lastlist = $list;
> $list = check_the_list() or last;
>
> my @gone;
> for (0..$#{$list}) {
> # if this entry is still there, move onto the next
> $list->[$_][0] eq $lastlist->[$_][0] and next;
>
> # remove this entry from $lastlist and try again
> push @gone, $lastlist->[$_][0];
> splice @$lastlist, $_, 1;
> redo;
> }
>
> my @changed =
> # we want the names...
> map { $list->[$_][0] }
> # ...of the entries that have changed
> grep { $list->[$_][1] != $lastlist->[$_][1] }
> 0..$#{$list};
>
> # do something with @changed and @gone
>
> sleep 5;
> }
>
> I used arrays above as your keys above don't seem to be unique... if
> they *are* unique, or if you can make them so, using a hash would be
> better, as the above is rather ugly (using array indices is always
> ugly), and make several important assumptions:
>
> The order of entries in the list will never change.
>
> The list never acquires any new entries.
>
> An entry will never replace another with the same name; that is, the
> list will never go from
>
> B 1
> A 2
> A 3
> C 4
>
> to
>
> B 1
> A 2
> C 4
>
> as you now cannot tell which 'A' it is has vanished.
>
> > Now I check the list every 5 seconds.
>
> And what difference does that make to anything?
>
> Ben

I think the way I framed the question was terrible.Let me put it in a
way others understand :)
Please find the code. The code is modified so that it has only
important sections.
Tha application has got a list which keeps updating. My original
question how to find out which one is changing.
Hope this time I m on target.

use Tk;
use strict;
use Win32::GuiTest qw(:ALL);

# Create a new window
my $mw = MainWindow->new;
#create a canvas object on the window
my $canvas = $mw->Canvas(-background,"white",-width,1024,-height,768);
$canvas->pack(-expand => 1, -fill => 'both');
$mw->repeat(5000,[\&updateScreen]);
MainLoop;


sub updateScreen()
{
$nofModels=ReadScheduler();

$canvas->delete('all');

//code for display
}

sub ReadScheduler
{
open(DATA,">$SchedFile") or die "dead duck";
my @whnds = FindWindowLike( undef, "xxxxxx" );
if( !@whnds )
{
die "Cannot find window with title/caption xxx\n";
}
my $mywndw= $whnds[ 0 ];
my ($listbox) = FindWindowLike($mywndw, "", "ListBox");
for (GetListContents($listbox))
{
print DATA $_ . "\n" ;
}
close($SchedFile);

$temp=0;

open(DATA,$SchedFile) or die "dead duck";
while()
{
print $_;
if($_=~/(\d+)\s+(\d+)\s+(\d+)/)
{
$sched_1 [$temp]=$1;
$sched_2[$temp]=$2;
$sched_3[$temp]=$3;
$temp++;
}
}
close($SchedFile);

return $temp;
}

Re: Monitoring a list?

am 04.09.2007 23:17:52 von Ben Morrow

Quoth jis :



> Please find the code. The code is modified so that it has only
> important sections.
> Tha application has got a list which keeps updating. My original
> question how to find out which one is changing.
> Hope this time I m on target.
>
> use Tk;

AFAICS, Tk is irrelevant to your problem, so your postd example should
not have included it. You are expected to keep removing things from your
program until you can't reproduce your problem.

> use strict;

Where is
use warnings;
?

> use Win32::GuiTest qw(:ALL);
>
> # Create a new window
> my $mw = MainWindow->new;
> #create a canvas object on the window
> my $canvas = $mw->Canvas(-background,"white",-width,1024,-height,768);
> $canvas->pack(-expand => 1, -fill => 'both');
> $mw->repeat(5000,[\&updateScreen]);
> MainLoop;
>
>
> sub updateScreen()
^^
Do you know what these parens do? I don't think you do. Don't put them
in.

> {
> $nofModels=ReadScheduler();
>
> $canvas->delete('all');
>
> //code for display

This is not a Perl comment. Did you actually run this code before
posting it?

> }
>
> sub ReadScheduler
> {
> open(DATA,">$SchedFile") or die "dead duck";

Don't use the DATA filehandle, Perl uses it for its own purposes. In
fact, don't use global bareword filehandles at all.

Use three-arg open, it's safer.

Include some helpful information in the error message.

open( my $DATA, '>', $SchedFile )
or die "can't write to '$SchedFile': $!";

> my @whnds = FindWindowLike( undef, "xxxxxx" );
> if( !@whnds )
> {
> die "Cannot find window with title/caption xxx\n";
> }

This can be simplified to

my @whnds = FindWindowLike( undef, "xxxxxx" )
or die "...";

in exactly the same way as the 'open or die' above.

> my $mywndw= $whnds[ 0 ];
> my ($listbox) = FindWindowLike($mywndw, "", "ListBox");
> for (GetListContents($listbox))
> {
> print DATA $_ . "\n" ;
> }
> close($SchedFile);
>
> $temp=0;

Global symbol "$temp" requires explicit package name at...

Did you actually run this code before you posted it?

> open(DATA,$SchedFile) or die "dead duck";

Now, why on *EARTH* are you writing your data out to a file and then
reading it all back in again? I can see the point if you are expecting
more data than you can fit in memory, but that doesn't seem likely in
this case: indeed, the for loop above has already had the whole lot in
memory. Just put whatever you want to do inside the for loop.

> while()
> {
> print $_;
> if($_=~/(\d+)\s+(\d+)\s+(\d+)/)
> {
> $sched_1 [$temp]=$1;

Where are these $schen_n variables defined?

Variables named in a sequence are always a sign you should be using a
data structure; in this case, an array:

$sched[1][$temp] = $1;

or you could probably use push and eliminate $temp altogether. I can't
tell without knowing what else you do with the contents of $sched_n, in
particular what it is initialized to before we get here.

> $sched_2[$temp]=$2;
> $sched_3[$temp]=$3;
> $temp++;
> }
> }
> close($SchedFile);
>
> return $temp;

I'm going to take a wild leap here, and guess that $sched_n are globals
used by '//code for display' above. It owuld be much better style to
return several values from ReadScheduler, so you would have

my $sched;

at the top of the sub,

return ($temp, $sched);

at the end (or simply

return $sched;

if, as I suspect, you can find out how many items there are by, err,
counting them), and call it as

my ($count, $sched) = ReadScheduler();

in updateScreen above.

Ben

Re: Monitoring a list?

am 05.09.2007 00:53:08 von jis

On Sep 4, 4:17 pm, Ben Morrow wrote:
> Quoth jis :
>
>
>
> > Please find the code. The code is modified so that it has only
> > important sections.
> > Tha application has got a list which keeps updating. My original
> > question how to find out which one is changing.
> > Hope this time I m on target.
>
> > use Tk;
>
> AFAICS, Tk is irrelevant to your problem, so your postd example should
> not have included it. You are expected to keep removing things from your
> program until you can't reproduce your problem.
>
> > use strict;
>
> Where is
> use warnings;
> ?
>
> > use Win32::GuiTest qw(:ALL);
>
> > # Create a new window
> > my $mw = MainWindow->new;
> > #create a canvas object on the window
> > my $canvas = $mw->Canvas(-background,"white",-width,1024,-height,768);
> > $canvas->pack(-expand => 1, -fill => 'both');
> > $mw->repeat(5000,[\&updateScreen]);
> > MainLoop;
>
> > sub updateScreen()
>
> ^^
> Do you know what these parens do? I don't think you do. Don't put them
> in.
>
> > {
> > $nofModels=ReadScheduler();
>
> > $canvas->delete('all');
>
> > //code for display
>
> This is not a Perl comment. Did you actually run this code before
> posting it?
>
> > }
>
> > sub ReadScheduler
> > {
> > open(DATA,">$SchedFile") or die "dead duck";
>
> Don't use the DATA filehandle, Perl uses it for its own purposes. In
> fact, don't use global bareword filehandles at all.
>
> Use three-arg open, it's safer.
>
> Include some helpful information in the error message.
>
> open( my $DATA, '>', $SchedFile )
> or die "can't write to '$SchedFile': $!";
>
> > my @whnds = FindWindowLike( undef, "xxxxxx" );
> > if( !@whnds )
> > {
> > die "Cannot find window with title/caption xxx\n";
> > }
>
> This can be simplified to
>
> my @whnds = FindWindowLike( undef, "xxxxxx" )
> or die "...";
>
> in exactly the same way as the 'open or die' above.
>
> > my $mywndw= $whnds[ 0 ];
> > my ($listbox) = FindWindowLike($mywndw, "", "ListBox");
> > for (GetListContents($listbox))
> > {
> > print DATA $_ . "\n" ;
> > }
> > close($SchedFile);
>
> > $temp=0;
>
> Global symbol "$temp" requires explicit package name at...
>
> Did you actually run this code before you posted it?
>
> > open(DATA,$SchedFile) or die "dead duck";
>
> Now, why on *EARTH* are you writing your data out to a file and then
> reading it all back in again? I can see the point if you are expecting
> more data than you can fit in memory, but that doesn't seem likely in
> this case: indeed, the for loop above has already had the whole lot in
> memory. Just put whatever you want to do inside the for loop.
>
> > while()
> > {
> > print $_;
> > if($_=~/(\d+)\s+(\d+)\s+(\d+)/)
> > {
> > $sched_1 [$temp]=$1;
>
> Where are these $schen_n variables defined?
>
> Variables named in a sequence are always a sign you should be using a
> data structure; in this case, an array:
>
> $sched[1][$temp] = $1;
>
> or you could probably use push and eliminate $temp altogether. I can't
> tell without knowing what else you do with the contents of $sched_n, in
> particular what it is initialized to before we get here.
>
> > $sched_2[$temp]=$2;
> > $sched_3[$temp]=$3;
> > $temp++;
> > }
> > }
> > close($SchedFile);
>
> > return $temp;
>
> I'm going to take a wild leap here, and guess that $sched_n are globals
> used by '//code for display' above. It owuld be much better style to
> return several values from ReadScheduler, so you would have
>
> my $sched;
>
> at the top of the sub,
>
> return ($temp, $sched);
>
> at the end (or simply
>
> return $sched;
>
> if, as I suspect, you can find out how many items there are by, err,
> counting them), and call it as
>
> my ($count, $sched) = ReadScheduler();
>
> in updateScreen above.
>
> Ben

That was really cool!!
Thanks for that....
here is

But i have got data very similiar to

A 10
B 20
A 5
C 20

sitting on a listbox in an application. The listbox data gets
decremented with time.Only one variable updates at a time. WHen it
reaches zero it disappears. A new one gets added up at some point of
time.

I read the application listbox every 5 sec (as u see in my
program)and manipulates the data for a different purpose. But i want
to know which variable is decrementing( A or B or C).

What could be the easiest way to find which one is on action?

cheers.
jis