in reply to Regex - counting number of '|'

One way to do it:
#!/usr/bin/env perl use warnings; use strict; my $inp_row = "per|l|mo|nks|ro|ck|s"; my $de = '|'; my @chars = split //, $inp_row; my $cnt = 0; for (@chars) { $cnt++ if ($_ eq $de) } print "delim is $de count is $cnt\n";

Prints:

delim is | count is 6

Replies are listed 'Best First'.
Re^2: Regex - counting number of '|'
by johngg (Canon) on Mar 17, 2008 at 21:45 UTC
    my @chars = split //, $inp_row; my $cnt = 0; for (@chars) { $cnt++ if ($_ eq $de) }

    You could eliminate the array, for loop and conditional increment by using grep.

    my $cnt = grep { $_ eq $de } split m{}, $inp_row;

    I hope this is of interest.

    Cheers,

    JohnGG

Re^2: Regex - counting number of '|'
by perl_junkie (Acolyte) on Mar 17, 2008 at 21:01 UTC
    This is awesome guys..!!! works like a charm...!!

    Thanks a lot Kyle, Toolic....!