http://www.perlmonks.org?node_id=943083

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

Hello Monks, i am new to perl programming. i am trying to write a program to find number of occurence of '\' in an array file path. I tried the follwing code,
@list = "C:\username\Sources"; foreach $arrchar (@list) { print "array char = $arrchar"; if($_ = /\\/) { print "\nfound one \n"; } }
but it was not working, i mean it is not printng "found one". but errors = 0 please excuse my ignorance if my code is wrong. Reading the manual is confusing me further.. can anyone please help me with the code?

Thanks,

Kumeperl

Replies are listed 'Best First'.
Re: search for '\' in perl
by moritz (Cardinal) on Dec 12, 2011 at 13:53 UTC

    There's no backslash in your string, which is why it doesn't find one. If you use warnings (as you should, especially as a beginner), it gives you a clue:

    $ perl -wE 'say "C:\username\Sources"' Unrecognized escape \S passed through at -e line 1. C:SernameSources

    The \ in the double-quoted strings have a special meaning. Use double backslash to in ""-literals to produce a backslash in the string.

Re: search for '\' in perl
by DStaal (Chaplain) on Dec 12, 2011 at 13:54 UTC

    The line if($_ = /\\/) is probably not doing what you want it to do. Either you mean if($_ =~ m/\\/) which would mean "if it contains '\'", or you mean if($_ eq '\') which would mean "if it is '\'".

    if($_ = /\\/) is attempting to assign something to $_, and testing if the assignment succeed. Probably not what you want.

Re: search for '\' in perl
by tinita (Parson) on Dec 12, 2011 at 14:11 UTC
    please read perlintro, Modern Perl and/or one of the other tutorials. you're doing wrong almost everything what could possibly go wrong =)

    you are not using warnings and strict.
    the element in @List will contain the string "C:SernameSources".
    you are trying to use $_ but $arrchar is the variable you want to do the matching on.
    by saying $_ = /.../ you are assigning to $_. you have to say $arrchar =~ /\\/
Re: search for '\' in perl
by Lotus1 (Vicar) on Dec 12, 2011 at 13:56 UTC

    Please add these to the top of your code:

    use strict; use warnings;

    You are using doublequotes for the path so the backslash '\' is interpreted as an escape character for the next character. Try using single quotes. Also read perlretut, the perl regular expresion tutorial, especially the section about the 'g' modifier. Let us know what errors you get if you get stuck after trying these.

Re: search for '\' in perl
by ricDeez (Scribe) on Dec 12, 2011 at 15:45 UTC

    This is probably what you want, taking into account the comments by the other monks:

    use strict; use warnings; use 5.012; my @list = ("C:\\username\\Sources"); for (@list) { say "array char = $_"; while ($_ =~ m/\\/g) { say "found a '$&'"; # holds the portion of the string that mat +ched } }
Re: search for '\' in perl
by TJPride (Pilgrim) on Dec 12, 2011 at 17:50 UTC
    use strict; use warnings; open (HANDLE, 'C:\username\Sources') || die; $_ = join '', <HANDLE>; my @m = m/\\/g; ### All matched strings print ('Found ' . ($#m + 1) . ' matches!');
      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!";