LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2005, 05:52 PM   #1
amytys
LQ Newbie
 
Registered: Sep 2004
Posts: 11

Rep: Reputation: 0
perl regular expression question (ARGHH!)


Suppose I want to find list all files starting w/ABC.

I can do the following:

opendir (DIR, ".") || print "directory doesnt exist\n";
@files = grep(/^ABC/,readdir(DIR));
foreach $file (@files){
if (-f "$sInputDir/$file"){
print "$file\n";
}
}


However, if that ABC is held in a variable, say $filemask, I can't get this code to work. I've tried escaping the $ and all.

Attempts include:

@files = grep(/^\$filemask/,readdir(DIR));
@files = grep(/^\\\$filemask/,readdir(DIR));
ect...


Anyone know how to get this to work?
 
Old 02-16-2005, 06:35 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Maybe it's a grep thing, I tend to use

Code:
while( defined( $file = readdir(DIR)) )
{
    next unless $file =~ /^ABC/ ;
    blah
}
although I haven't tried pattern as a var
 
Old 02-17-2005, 07:22 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You could pipe find command too, like :

Code:
$search = "ABC*";
$dir       = ".";
open FIND, "find $dir -name '$search' |";

while(<FIND>) { print; }
close FIND;
 
Old 02-18-2005, 03:31 PM   #4
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
put double qoutes around $filemask. I.E: "$filemask"
 
Old 02-21-2005, 06:29 AM   #5
Linux_in_NH
Member
 
Registered: Jan 2004
Location: NH
Distribution: Mandrake, Geentoo, Ubuntu
Posts: 105

Rep: Reputation: 15
Try making filemask = a regular expression variable, i.e.

$filemask=qr/^ABC/;

then

@files = grep($filemask,readdir(DIR));
 
Old 02-22-2005, 03:59 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
How about?

Code:
for $file (<w/ABC*>) {

    print "$file\n";
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl regular expression problem true_atlantis Programming 4 05-27-2009 06:35 AM
Perl regular expression issue zikhermm Programming 7 09-23-2005 03:48 PM
Perl Regular Expression dilemma GATTACA Programming 1 03-27-2004 07:48 PM
Perl Regular Expression rch Programming 14 07-11-2003 11:00 PM
using a perl regular expression in php markus1982 Programming 5 11-18-2002 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration