Nick
Nick

Reputation: 5

How to get rid of the empty string in the array in perl?

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

Upvotes: 1

Views: 366

Answers (4)

simbabque
simbabque

Reputation: 54381

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.

Upvotes: 5

Dave Sherohman
Dave Sherohman

Reputation: 46245

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

Upvotes: 0

cdarke
cdarke

Reputation: 44434

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

Upvotes: 1

Guru
Guru

Reputation: 17054

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.

Upvotes: 1

Related Questions