1

I have a string of many zeroes and '1' but I just want the value of zeroes from the string. So, I use split function to split the '1' but it seems like it will produce the undef or empty string in the array. So, I try to scan through each array element using foreach and compare if there is empty string that i thought was "undef" will be ignored. In return, those in the not empty string will not be ignored and will be placed into another variable. The problems seem like it does not recognized my "undef" variable.

OR there is another better method to scan the string in order to pull off the number of '0' in the string?

Please help out on this matters. This the coding:

#!/usr/bin/perl

  use strict;
 use warnings;

  my $data = '111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000011111
';

  my @values = split('1', $data);
  my $zero = "0";
foreach my $val (@values)
{
    if (!defined$val)
    {
      $zero= $val;
     }

}
  print "$zero\n";

  exit 0;

Thanks a lot.

Regards, Nicky

4 Answers 4

5

Try using tr. It transliterates all occurrences of 0 to an empty string and returns the number of changes it made. That's your number of zeroes.

my $data = '111100';
my $count = $temp =~ tr/0//;
print $count;

Take a look at this blog post for a benchmark of different approaches.

Sign up to request clarification or add additional context in comments.

4 Comments

You can use tr/0/0/ to avoid modifying the original string: perl -E '$d = 10101; say $d =~ tr/0/0/; say $d; output: 2, 10101
It doesn't modify the string. Use your sample line with tr/0//. You'd need tr/0//d to tell it to remove the zeros. (Zeroes?)
@JimDavis you're right, thank you. I edited my answer. Wiktionary says both zeros and zeroes is correct. My Collins says the same, no AE/BE here, just both. ;-)
@user1799941 what kind of error? Post the error message please.
1
my $data = '111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000011111
';       
my @arr=$data=~/0/g;
print scalar(@arr);

Using the matching operation, all 0's can be fetched into an array, and the length of the array will give the count of zero's.

2 Comments

You could also use the goatse-operator to shorten it: my $count =()= $data =~ m/0/g;
Thanks for sharing .. this really solve the question. Glad that you help :D
1

Not certain precisely what you need, but a regular expression might be useful:

#!/usr/bin/perl
use strict;
use warnings;

my $data = '111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000011111';

# If you want an array of zeros

my @zeros = $data =~ /0/g;
print "@zeros\n";

# If you want the zeros together

my @multizeros = $data =~ /0+/g;
print "@multizeros\n";

Gives:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
000000000000000

Comments

0

Although a regex match will probably be the most efficient way to do this, as Guru and cdarke already suggested, you can do it with split by splitting on groups of 1s instead of single 1s:

$d = 101111001;
say scalar split /1+/, $d;    # prints 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.