Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a small array of strings holding zeros(0) as placeholders
my @p; my @pos = qw(1 2 3 4); foreach (@pos) { $p[$_] = '0' x 128; }
and I want to set the char to one(1) if I have the need to hold that position
my @ranges = qw(1..4 5..8 9..12 13..16 1..8 9..16 ) foreach my $range ( @ranges ) { foreach($range){ substr($str1,$_,1) = '1'; } }
Thats fine but I need to test if the nums are already set to '1' so I move to a different string that's not set yet i.e., I don't want to clobber an existing range that has a placeholder. That's where I'm stuck. How to test a range of chars in a string to see if the value is '1'. I don't know why I'm stuck. Does this question make sense? Thanks.

Replies are listed 'Best First'.
Re: strings as placeholders
by runrig (Abbot) on Nov 15, 2001 at 02:07 UTC
    my @ranges = qw(1..4 5..8 9..12 13..16 1..8 9..16 )
    That's not going to do what you want (and you forgot a semi-colon on the end). I think you want:
    my @ranges = ([1..4],[5..8],[9..12],[13..16],[1..8],[9..16]); foreach my $range (@ranges) { foreach (@$range) {...
    As for your problem, I'm not sure if I understand it, but my guess is to go through the range a first time and check for 1's, and skip to the next iteration if there's any one's.
Re: strings as placeholders
by kwoff (Friar) on Nov 15, 2001 at 02:11 UTC
    Look into `perldoc -f vec` and maybe use a bit vector instead. I think it's what you're looking for.
      I read the perldoc yopu suggested and I don't think I'm advanced enough for that yet. Thanks though.
        Here's an example of using vec():
        #!/usr/local/bin/perl -w use strict; # a bit string my $bits = ''; # set some of the bits my @offsets = (0, 2, 4, 6); for my $offset (@offsets) { vec($bits, $offset, 1) = 1; } # test which bits are set my @bits = (0 .. 10); for my $bit (@bits) { if (vec($bits, $bit, 1)) { print "bit $bit is set\n"; } else { print "bit $bit is not set\n"; } } __END__
        $ ./vec-demo.pl bit 0 is set bit 1 is not set bit 2 is set bit 3 is not set bit 4 is set bit 5 is not set bit 6 is set bit 7 is not set bit 8 is not set bit 9 is not set bit 10 is not set