search for '\' in PERL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumeperl
    New Member
    • Dec 2011
    • 2

    search for '\' in PERL

    Hello All,
    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,
    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
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Check out perlfaq4 - How can I count the number of occurrences of a substring within a string:

    Code:
    use strict;
    use warnings;
    
    my @list = "C:\\username\\Sources";
    
    for my $arrchar (@list) {
    	print "array element = $arrchar";
    	my $slashes = () = $arrchar =~ /\\/g;
    	if ($slashes) {
    		print "\nfound $slashes\n";
    	}
    }
    - Miller

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Just to get what you had pasted working needed some changes, as well as the wonderful addition that Miller made. You really should reference a Perl Regex tutorial. Also, please "use strict;" and "use warnings;". If you had, you would have been informed of the errors that Perl found in execution.

      Code:
      #!/usr/bin/perl
      
      use strict;
      use warnings;
      
      my @list = 'C:\username\Sources';
       
      foreach my $arrchar (@list)
      {
         print "array char = $arrchar";
         if($arrchar =~ m/\\/)
                     {
                          print "\nfound one \n";
                     }
      }

      Comment

      • kumeperl
        New Member
        • Dec 2011
        • 2

        #4
        Thanks for your fast response. after taking your suggestions i did the following changes in code
        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\Sou rces

        found one

        value of count = 1

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

        kumeperl

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          Code:
          #!/usr/bin/perl
           
          use strict;
          use warnings;
           
          my $str = 'C:\username\Sources';
          
          # this is the most efficient
          my $count = ($str =~ tr/\\//);
          print "count is $count\n";
          
          # or you could do this
          my $cnt = () = $str =~ /\\/g;
          print "count is $cnt\n";

          Comment

          • miller
            Recognized Expert Top Contributor
            • Oct 2006
            • 1086

            #6
            kumeperl,

            Look at my earlier post and link to perlfaq4. You're currently only testing for the existence of a slash in the string, not for the exact count.

            - Miller

            Comment

            Working...