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:- -
use Data::Dumper;
-
#use strict;
-
#use warnings;
-
use File::Find;
-
use File::Stat;
-
use Time::Local;
-
-
my $test_data1='C:\perl_practice\Folder';
-
-
my @cbr;
-
-
@cbr = $test_data1;
-
-
foreach $cbr (@cbr) {
-
opendir(DIR,$cbr);
-
@files = readdir(DIR);
-
closedir(DIR);
-
foreach $file (sort @files) {
-
print("$file \n");
-
}
-
}
-
When I execute this program.All the files available in the C:\perl_practice\Folder are diplayed. -
plat_USb
-
ICON_SIM
-
MYCO_SIM
-
SAME_SIM
-
TIME_cable
-
GURU_JA
-
JVA_TEX
-
But I want display only those files which contain SIM word in the file name.
like -
ICON_SIM
-
MYCO_SIM
-
SAME_SIM
-
Waiting for ur valuable response
Regards,
Sada
5 2093
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. -
use strict;
-
-
my $test_data1='C:/perl_practice/Folder';
-
-
my @cbr = $test_data1;
-
-
foreach my $cbr (@cbr) {
-
opendir(DIR, $cbr) or die "Can't open $some_dir: $!";
-
my @files = readdir(DIR);
-
closedir(DIR);
-
-
foreach my $file (sort @files) {
-
print "$file \n" if $file =~ /SIM$/;
-
}
-
}
-
- Miller
prn 254
Expert 100+
Another plausible idiom: -
use strict;
-
-
my $pattern = "SIM";
-
my $test_data1='C:/perl_practice/Folder';
-
-
my @cbr = $test_data1;
-
-
foreach my $cbr (@cbr) {
-
opendir (DIR, $cbr) or die "Could not open $cbr for read: $! \n";
-
@files = grep /$pattern/, readdir(DIR);
-
closedir (DIR);
-
-
foreach my $file (sort @files) {
-
print "$file \n";
-
}
-
}
-
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
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
in the spirit of TIMTOWTDI
use strict;
my $pattern = "SIM";
my $test_data1='C:/perl_practice/Folder/';
print "$_\n" for sort <$test_data1*$pattern>;
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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?
...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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;...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |