473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Pattern Matching

I have a code which displays the list of files avaialble in a particular folder.

Example:-

I have 123,123_SIM,456_SIM,678_SIM,890 files in the following folder.
C:\perl_practice\Folder

Code:-

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2. #use strict;
  3. #use warnings;
  4. use File::Find;
  5. use File::Stat;
  6. use Time::Local;
  7.  
  8. my $test_data1='C:\perl_practice\Folder';
  9.  
  10. my @cbr;
  11.  
  12. @cbr = $test_data1;
  13.  
  14. foreach $cbr (@cbr) {
  15.     opendir(DIR,$cbr);
  16.     @files = readdir(DIR);
  17.     closedir(DIR);
  18.     foreach $file (sort @files) {
  19.         print("$file \n");
  20.     }
  21. }
  22.  
When I execute this program.All the files available in the C:\perl_practice\Folder are diplayed.

Expand|Select|Wrap|Line Numbers
  1. plat_USb
  2. ICON_SIM
  3. MYCO_SIM
  4. SAME_SIM
  5. TIME_cable
  6. GURU_JA
  7. JVA_TEX
  8.  
But I want display only those files which contain SIM word in the file name.
like

Expand|Select|Wrap|Line Numbers
  1. ICON_SIM
  2. MYCO_SIM
  3. SAME_SIM
  4.  
Waiting for ur valuable response

Regards,
Sada
May 22 '07 #1
5 2093
miller
1,089 Expert 1GB
Hi Sada,

First off, never comment out "use strict;". It's during development that you need it most, so do yourself a favor and always leave it in.

Cleaning up your code a little bit.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $test_data1='C:/perl_practice/Folder';
  4.  
  5. my @cbr = $test_data1;
  6.  
  7. foreach my $cbr (@cbr) {
  8.     opendir(DIR, $cbr) or die "Can't open $some_dir: $!";
  9.     my @files = readdir(DIR);
  10.     closedir(DIR);
  11.  
  12.     foreach my $file (sort @files) {
  13.         print "$file \n" if $file =~ /SIM$/;
  14.     }
  15. }
  16.  
- Miller
May 22 '07 #2
prn
254 Expert 100+
Another plausible idiom:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $pattern = "SIM";
  4. my $test_data1='C:/perl_practice/Folder';
  5.  
  6. my @cbr = $test_data1;
  7.  
  8. foreach my $cbr (@cbr) {
  9.     opendir (DIR, $cbr) or die "Could not open $cbr for read: $! \n";
  10.     @files = grep /$pattern/, readdir(DIR);
  11.     closedir (DIR);
  12.  
  13.     foreach my $file (sort @files) {
  14.         print "$file \n";
  15.     }
  16. }
  17.  
The potential advantage of using grep to limit the array of files is that @files now contains only the files you are interested in and you don't have to limit them again. Whether this is an actual advantage depends on what you want to do.

As usual, there's more than one way to do it. :)

HTH,
Paul
May 22 '07 #3
prn
254 Expert 100+
OBTW, you (sada) initially said:
I have a code which displays the list of files avaialble in a particular folder.
But the code you gave looks like it is intended to search through a larger number of folders and you just gave one of them. Now this is plausible in a demo, but just in case you actually meant "a particular folder" rather than "a number of folders" you could simplify your code considerably by eliminating @cbr, the array of folders. If you are just simplifying and meant a bunch of folders, then never mind.

Best Regards,
Paul
May 22 '07 #4
KevinADC
4,059 Expert 2GB
in the spirit of TIMTOWTDI

use strict;
my $pattern = "SIM";
my $test_data1='C:/perl_practice/Folder/';
print "$_\n" for sort <$test_data1*$pattern>;
May 22 '07 #5
prn
254 Expert 100+
Oh, yeah! Nice one, Kevin!

For Sada: to understand Kevin's version, read about the "Filename Globbing Operator" (pp. 83-85 in the Camel Book 3rd edition).

<runs off to think about the whys and wherefores of readdir vs. filename globbing>

:)

Paul
May 23 '07 #6

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

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
9
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
1
by: VanKha | last post by:
I write this program for pattern-matching,but it gives wrong result: #include<iostream> #include<conio.h> #include<string.h> using namespace std; main() { char text,pat;...
5
by: pramodkh | last post by:
Hi All I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line. for example, I have the following content in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.