Ways to test for membership? 
Author Message
 Ways to test for membership?

Does anyone know how to implement membership tests?  I'm trying to do stuff like

{
        blah blah blah

Quote:
}

I've tried a couple different ways with no success.  Thanks.
--
 David Gabrius                   my opinions are my own, not Motorola's!

 "And you can find/Your own way out/You can build/And I can will..." -U2
 "You miss too much these days if you stop to think..." -U2  


Sun, 10 Dec 1995 22:12:51 GMT  
 Ways to test for membership?

Quote:

>Does anyone know how to implement membership tests?  I'm trying to do stuff like

>{
>    blah blah blah
>}

>I've tried a couple different ways with no success.  Thanks.
>--
> David Gabrius                   my opinions are my own, not Motorola's!

> "And you can find/Your own way out/You can build/And I can will..." -U2
> "You miss too much these days if you stop to think..." -U2  

for string values you can grep a regexp (which allows pattern matching,
case insensitivity etc.)


or you can compare the string directly


or you can do any operation on $_ which yeilds a (perl) true value for the
elements you're interested in.  Grep returns (in this scalar context case)
the number of matches, if it is in array context then an array comprising

Hope this helps,

Mike

--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | Reservoir Place

Meiko tel: (617) 890 7676         | Waltham, MA 02154



Mon, 11 Dec 1995 03:02:22 GMT  
 Ways to test for membership?

Quote:

>Does anyone know how to implement membership tests?  I'm trying to do stuff like

>{
>    blah blah blah
>}

>I've tried a couple different ways with no success.  Thanks.

Mike> for string values you can grep a regexp (which allows pattern matching,
Mike> case insensitivity etc.)

Mike> or you can compare the string directly

Mike> or you can do any operation on $_ which yeilds a (perl) true value for the
Mike> elements you're interested in.  Grep returns (in this scalar context case)
Mike> the number of matches, if it is in array context then an array comprising

But neither of these are very fast if you are finding a fixed string
in a set of many strings repeatedly.  The proper answer (similar to
something in the Camel book, although I don't have my Camel with me)
is:

        ## initialize...

        ## for each $var...
        if ($my_set{$var}) {

        }


--
Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095

Quote: "Welcome to Portland, Oregon, home of the California Raisins!"



Sun, 10 Dec 1995 21:14:17 GMT  
 Ways to test for membership?

Quote:
(David M. Gabrius) writes:
> Does anyone know how to implement membership tests?  I'm
> trying to do stuff like

> {
>    blah blah blah
> }

Use associative arrays!  They are implemented with a hash table, which
is much faster than the linear search suggested by another respondent.

Assuming that all you want is fast membership testing:

# To put $var INTO the list
$list{$var} = 1;

# To check if $var is in the list
if ($list{$var} == 1) {
  blah; blah; blah;  #you forgot the semicolons ;-)

Quote:
}

# to remove $var from the list
$list{$var} = undef;

# or you could do it this way if you might later on put $var back into
# the list
$list{$var} = 0;


It's not a system called 'X Windows', it's a window system named 'X',
after the owl on "Mr. Rogers' Neighborhood".



Mon, 11 Dec 1995 04:59:04 GMT  
 Ways to test for membership?
        So here I am, trying to build a working version of perl 4.036
for a 486 box running (beta) Solaris 2.1.  Configure does reasonable
things, and the compilation completes with only a small number of
warnings using the Sun SUNWspro compiler tools.
        As soon as I try to run the tests, the perl script that runs
them itself causes a core-dump in perl.  It turns out the problem is
a deref of a NULL pointer down in "eval()" in eval.c.  The comment
right above this particular case in the switch statement (O_GOTO) says
"/* shudder */".  I can bandage the code to avoid the deref and to
continue along doing what looks reasonable, but tracing the debug dump
shows that perl reaches this point and then goes into an infinite loop
stress testing the Solaris 486 implementation of setjmp/longjmp.  I can
try to really figure out where the error is occuring---this switch is
within a loop that has an upper bound for the index of 114 and the value
is over three million when the core dump occurs, but I am not e{*filter*}d
about the prospect.  For one thing, various cases of the switch *change*
the value of the loop index, sometimes to a value retrieved from an
array.  As Randall might say, "blech".
        Has anyone *really* gotten Perl to build and work under Solaris
486?  If so, could you mail me the incantation?  We are supposed to get
the real release of 2.1 at the end of this week, and if it doesn't get
mysteriously better, I probably have a long haul ahead of me with my
nose in dbx.

        Thanks in advance,
        Richard



Mon, 11 Dec 1995 06:34:34 GMT  
 Ways to test for membership?

Quote:
># to remove $var from the list
>$list{$var} = undef;

It doesn't.  It just assign $list{$var} to undef.
You can see that the key still remains by saying:
        print join("\n", keys %list);

Use 'delete' to remove specified value from the assoc array.
--
Toru "devil-may-care" Shiono          Sony Corporation, JAPAN



Mon, 11 Dec 1995 14:32:19 GMT  
 Ways to test for membership?

Quote:
Gabrius) writes:
>Does anyone know how to implement membership tests?  I'm trying to do
>stuff like



Quote:
(Randal L. Schwartz) writes:


The following works too.  It doesn't count how many times each key
occurs, but if it's a normal set rather than a multiset, you don't care.


I ran the test program below on a CONVEX C2 running perl 4.34, with
unique keys and with each key repeated three times.  The mcdaniel
version:

was 2.3--3.1 times faster than the merlyn version:

and both methods used the same amount of memory in the test program.

Gory details: With the mcdaniel table building done first, the output is
(give or take epsilon)

    mcdaniel:
      PID TT  STAT   TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM
    16519 o2  S      0:02  0  9    684 2868 2772   xx  568 488 29.4  1.2
      PID TT  STAT   TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM
    16519 o2  S      0:03  0 14    980 4048 3952   xx  568 504 29.0  1.7
    merlyn:
      PID TT  STAT   TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM
    16519 o2  R      0:10  1 29   1613 6640 6544   xx  568 504 38.5  2.7
      PID TT  STAT   TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM
    16519 o2  S      0:12  0 36   1613 6640 6544   xx  568 504 42.8  2.7
    merlyn  : user: 3.35   system: 0.03
    mcdaniel: user: 1.32   system: 0.13
    #keys: 24495

If I swapped code blocks to make the merlyn table building go first, the
output for a sample run is

    merlyn:
      PID TT  STAT   TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM
    16960 o2  S      0:02  1 13    684 2868 2772   xx  568 488 22.3  1.2
      PID TT  STAT   TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM
    16960 o2  R      0:05  0 23    980 4048 3952   xx  568 504 29.2  1.7
    mcdaniel:
      PID TT  STAT   TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM
    16960 o2  S      0:12  0 44   1604 6604 6508   xx  568 504 29.7  2.7
      PID TT  STAT   TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM
    16960 o2  R      0:13  0 47   1604 6604 6508   xx  568 504 34.6  2.7
    merlyn  : user: 3.12   system: 0.20
    mcdaniel: user: 1.37   system: 0.02
    #keys: 24495

I presume the approximately 0.15 sec system time is for memory
allocation in whichever method is done first.  The method that follows
apparently need not go to the system for memory.  I thought that the

1s.  However, the SIZE ps field, "Virtual size of the process in 1-Kbyte
units ... the text segment is not included", shows that the first method
executed apparently goes from 2868K to 4048K and the second doesn't
allocate more memory, no matter which of the two methods is first.

I added

to see about duplications, and got, for mcdaniel first,

    mcdaniel:
      PID TT  STAT   TIME SL RE PAGEIN  SIZE   RSS  LIM TSIZ TRS %CPU %MEM
    18371 o2  R      0:08  0 32   1989  8912  8412   xx  568 488 28.4  3.5
      PID TT  STAT   TIME SL RE PAGEIN  SIZE   RSS  LIM TSIZ TRS %CPU %MEM
    18371 o2  S      0:11  1 48   2198  9744  9244   xx  568 504 25.4  3.9
    merlyn:
      PID TT  STAT   TIME SL RE PAGEIN  SIZE   RSS  LIM TSIZ TRS %CPU %MEM
    18371 o2  S      0:16  0 67   2423 10644 10144   xx  568 504 28.9  4.2
      PID TT  STAT   TIME SL RE PAGEIN  SIZE   RSS  LIM TSIZ TRS %CPU %MEM
    18371 o2  S      0:25  1 99   2423 10644 10144   xx  568 504 31.0  4.2
    merlyn  : user: 9.30   system: 0.07
    mcdaniel: user: 3.03   system: 0.15
    #keys: 24495

and merlyn first:

    merlyn:
      PID TT  STAT   TIME SL RE PAGEIN  SIZE   RSS  LIM TSIZ TRS %CPU %MEM
    17604 o2  S      0:08  0 26   1989  8912  8412   xx  568 488 33.7  3.5
      PID TT  STAT   TIME SL RE PAGEIN  SIZE   RSS  LIM TSIZ TRS %CPU %MEM
    17604 o2  S      0:16  0 57   2198  9744  9244   xx  568 504 30.5  3.9
    mcdaniel:
      PID TT  STAT   TIME SL RE PAGEIN  SIZE   RSS  LIM TSIZ TRS %CPU %MEM
    17604 o2  S      0:21  1 74   2423 10644 10144   xx  568 504 32.2  4.2
      PID TT  STAT   TIME SL RE PAGEIN  SIZE   RSS  LIM TSIZ TRS %CPU %MEM
    17604 o2  R      0:24  1 82   2423 10644 10144   xx  568 504 36.5  4.2
    merlyn  : user: 7.85   system: 0.22
    mcdaniel: user: 3.20   system: 0.03
    #keys: 24495

Similar memory results.

The program:

--- cut here and your tube will implode ---
#! /usr/local/bin/perl

$, = ' ';
$" = ' ';



print "mcdaniel:\n";
undef %my_set;


print "merlyn:\n";
undef %my_set;



printf "merlyn  : user: %.2f   system: %.2f\n",
    $merlyn_end[0] - $merlyn_start[0],
    $merlyn_end[1] - $merlyn_start[1];

printf "mcdaniel: user: %.2f   system: %.2f\n",
    $mcdaniel_end[0] - $mcdaniel_start[0],
    $mcdaniel_end[1] - $mcdaniel_start[1];


    print "Whoa! Different keys!\n";
    exit 1;

Quote:
}



    if ($mcdaniel_keys[$i] ne $merlyn_keys[$i]) {
        print "Whoa! Keys $i differ --\n",
            $mcdaniel_keys[$i], $merlyn_keys[$i];
        exit 1;
    }
Quote:
}

exit 0;

--
Tim McDaniel, Convex Computer Corporation, Richardson, TX



Wed, 13 Dec 1995 10:08:25 GMT  
 Ways to test for membership?

[some nice stats deleted]


I think I'd be more comfortable with:


--tom



Fri, 15 Dec 1995 22:27:17 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Test for membership of an array in Perl?

2. Beginner's Question: testing array membership

3. test for list membership - searching for the right idiom

4. test for membership in an array?

5. easier way to determine set membership?

6. Determining String Membership

7. newbie question: membership script w/ credit card verification

8. Module to check novell group membership?

9. Most popular membership management for small sites?

10. array membership

11. NT login script to query group membership....

12. Membership management

 

 
Powered by phpBB® Forum Software