[comp.lang.perl] vec problems

marcus@eecs.cs.pdx.edu (M. Daniels) (08/30/90)

I am using a large vector (from 860-1200 bits) as a key to an
associative array (its a simple way to find unique records).  Anyway,
what I want to do is take the length of the vector and find the first
active bit (so larger vectors of the same value can be compared).
If I do a for loop down from 8*length($vec) to 0, stopping when
vec($vec,$x,1)!=0 everything works great, but it seems
like it would be better to go by 8s.  However, when I do this, vec
only returns zeros.  huh?  I'm sure this is really obvious.  The docs
say BITS must be a power of 2 from 1 to 32, but 8 is.

thoughts, anecdotes, anything?

thanks

marcus daniels
-------------------------------------------------------------------------------
- MD -  marcus@eecs.ee.pdx.edu OR ....!uunet!tektronix!psueea!eecs!marcus
- MD -					 "An empty stomach is not a good polotical advisor."
-------------------------------------------------------------------------------

merlyn@iwarp.intel.com (Randal Schwartz) (08/30/90)

In article <10@pdxgate.UUCP>, marcus@eecs (M. Daniels) writes:
| 
| I am using a large vector (from 860-1200 bits) as a key to an
| associative array (its a simple way to find unique records).  Anyway,
| what I want to do is take the length of the vector and find the first
| active bit (so larger vectors of the same value can be compared).
| If I do a for loop down from 8*length($vec) to 0, stopping when
| vec($vec,$x,1)!=0 everything works great, but it seems
| like it would be better to go by 8s.  However, when I do this, vec
| only returns zeros.  huh?  I'm sure this is really obvious.  The docs
| say BITS must be a power of 2 from 1 to 32, but 8 is.
| 
| thoughts, anecdotes, anything?

Yeah.  (since you said "anything"... :-)

This seemed to work...

srand(time+$$);
$_ = "\0" x 400; # gimme 3200 bits
vec($_,rand(3200),1) = 1; # flip a random bit
OUTER: for $byte (0..(length($_)-1)) {
	if (vec($_,$byte,8) != 0) {
		for $bit (($byte*8)..($byte*8+7)) {
			if (vec($_,$bit,1) != 0) {
				print "bit is $bit\n";
				last OUTER;
			}
		}
	}
}

But, as I look at it, there's a much faster way...

srand(time+$$);
$_ = "\0" x 400; # gimme 3200 bits
vec($_,rand(3200),1) = 1; # flip a random bit
if (/[^\0]/) {
	$byte = length($`);
	for $bit (($byte*8)..($byte*8+7)) {
		if (vec($_,$bit,1) != 0) {
			print "bit is $bit\n";
			last;
		}
	}
}

How's that for the right tool for the right job? :-)

print "Just another bit of a Perl hacker,"
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/