473,471 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

search for '\' in PERL

2 New Member
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,
Expand|Select|Wrap|Line Numbers
  1.  
  2. @list = "C:\username\Sources"
  3.  
  4. foreach $arrchar (@list)
  5. {
  6.    print "array char = $arrchar";
  7.    if($_ = /\\/)
  8.                {
  9.                     print "\nfound one \n";
  10.                }
  11. }
  12.  
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
Dec 12 '11 #1
5 1767
miller
1,089 Recognized Expert Top Contributor
Check out perlfaq4 - How can I count the number of occurrences of a substring within a string:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my @list = "C:\\username\\Sources";
  5.  
  6. for my $arrchar (@list) {
  7.     print "array element = $arrchar";
  8.     my $slashes = () = $arrchar =~ /\\/g;
  9.     if ($slashes) {
  10.         print "\nfound $slashes\n";
  11.     }
  12. }
  13.  
- Miller
Dec 12 '11 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3.  
  4. use strict;
  5. use warnings;
  6.  
  7. my @list = 'C:\username\Sources';
  8.  
  9. foreach my $arrchar (@list)
  10. {
  11.    print "array char = $arrchar";
  12.    if($arrchar =~ m/\\/)
  13.                {
  14.                     print "\nfound one \n";
  15.                }
  16. }
  17.  
Dec 13 '11 #3
kumeperl
2 New Member
Thanks for your fast response. after taking your suggestions i did the following changes in code
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $count;
  7. my @list = 'C:\username\Sources';
  8.  
  9. chomp @list;
  10. #print OUTPTR "$_\n" foreach @list;
  11. foreach my $arrchar (@list)
  12. {
  13.    print "array char = $arrchar";
  14.    if($arrchar =~ m/\\/)
  15.                {
  16.                     print "\nfound one \n";
  17.                     $count += 1;
  18.                }
  19.    else
  20.    {
  21.       print"\n i am in else \n"
  22.    }
  23. }
  24. print "value of count = $count";
  25.  
  26.  
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
Dec 13 '11 #4
RonB
589 Recognized Expert Moderator Contributor
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $str = 'C:\username\Sources';
  7.  
  8. # this is the most efficient
  9. my $count = ($str =~ tr/\\//);
  10. print "count is $count\n";
  11.  
  12. # or you could do this
  13. my $cnt = () = $str =~ /\\/g;
  14. print "count is $cnt\n";
Dec 13 '11 #5
miller
1,089 Recognized Expert Top Contributor
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
Dec 13 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: R. Rajesh Jeba Anbiah | last post by:
Q: Is PHP search engine friendly? Q: Will search engine spiders crawl my PHP pages? A: Spiders should crawl anything provided they're accessible. Since, nowadays most of the websites are been...
1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
5
by: George | last post by:
Hi, Anyone has the background for explaining? I have made a search on my name and I have got a link to another search engine. The link's title was the search phrase for the other search engine...
3
by: Alastair | last post by:
Hello guys, I've been building a search facility for an intranet site I'm part of developing and we've been building a search engine using Index Server. It mostly works, however there have been...
39
by: Noticedtrends | last post by:
Can inference search-engines narrow-down the number of often irrelevant results, by using specific keywords; for the purpose of discerning emerging social & business trends? For example, if...
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
0
by: passion | last post by:
"Specialized Search Engines" along with Google Search Capability (2 in 1): http://specialized-search-engines.blogspot.com/ Billions of websites are available on the web and plenty of extremely...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.