in reply to search for '\' in perl

use strict; use warnings; open (HANDLE, 'C:\username\Sources') || die; $_ = join '', <HANDLE>; my @m = m/\\/g; ### All matched strings print ('Found ' . ($#m + 1) . ' matches!');

Replies are listed 'Best First'.
Re^2: search for '\' in perl
by kumeperl (Initiate) on Dec 13, 2011 at 13:04 UTC
    Hello monks, Thanks for your fast response. after taking your suggestions i did the following changes in code
    #!/usr/bin/perl -w use strict; use warnings; my $count; my @list = 'C:\username\Sources'; chomp @list; #print OUTPTR "$_\n" foreach @list; foreach my $arrchar (@list) { print "array char = $arrchar"; if($arrchar =~ m/\\/) { print "\nfound one \n"; $count += 1; } else { print"\n i am in else \n" } } print "value of count = $count";

    the result is

    array char = C:\username\Sources

    found one

    value of count = 1

    i am expecting count = 2. again i am not sure why it is not working properly.

    regards,

    kumeperl

      I read that page a while ago, threw me off because the $count = (my pattern matching) format didn't seem to work. But apparently, $count = () = pattern match does, cool! I learn something new every day:

      use strict; use warnings; open (HANDLE, 'C:\username\Sources') || die; $_ = join '', <HANDLE>; my $c = () = m/\\/g; print "Found $c matches!";