CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
glob and regex

 



DoolinDalton
Novice

Sep 24, 2009, 2:41 PM

Post #1 of 4 (3321 views)
glob and regex Can't Post

I have the following very simple code.

@files = glob("C:\\test\\*");
$file_count = @files;
print $file_count;

As you can see, I am globbing everything in the directory C:\test and printing the count of the files. How do I utilize regex to modify the parameter of the glob so that...

(1) I exclude any files that containt the underscore character ( "_" )
(2) But include certain specific files that have the underscore character.

For example, if the file list in the directory was

file1.txt
file_2.txt
file3.txt
include_this_file.txt

Then I want to print 3. (Eventhough the last file has the underscore character, I want to include this in the glob.)

I would guess, somewhere in the regex I would need to hard code the files I want, in this case, the file called include_this_file.txt.


(This post was edited by DoolinDalton on Sep 24, 2009, 2:43 PM)


toolic
User

Sep 24, 2009, 4:44 PM

Post #2 of 4 (3313 views)
Re: [DoolinDalton] glob and regex [In reply to] Can't Post

To satisfy your 1st requirement, you could use grep to
filter out files with underscores:


Code
@files = grep { ! /_/ } glob("C:\\test\\*");


You need to refine your 2nd requirement
since it contradicts your 1st requirement.

You might be able to wedge something like this
into a grep as well:

Code
@files = glob("C:\\test\\*"); 
@keeps;
for (@files) {
if (($_ eq 'include_file.txt') or (!/_/) ){
push @keeps $_;
}
}


Since glob will also return subdirectoires, you may
also want to grab just files using -f:


Code
@files = grep { -f and (! /_/) } glob("C:\\test\\*");



(This post was edited by toolic on Sep 24, 2009, 4:52 PM)


shawnhcorey
Enthusiast


Sep 24, 2009, 4:51 PM

Post #3 of 4 (3312 views)
Re: [DoolinDalton] glob and regex [In reply to] Can't Post


In Reply To
For example, if the file list in the directory was

file1.txt
file_2.txt
file3.txt
include_this_file.txt

Then I want to print 3. (Eventhough the last file has the underscore character, I want to include this in the glob.)

I would guess, somewhere in the regex I would need to hard code the files I want, in this case, the file called include_this_file.txt.


I wouldn't try to do it with one complex expression but expand it out:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my @Valid_files = qw( include_this_file.txt );
my $file_count = 0;

for my $file ( glob( "C::\\test\\*" ) ){
if( $file =~ /_/ ){
for my $valid_file ( @Valid_files ){
if( $file =~ /\Q$valid_file\E$/ ){
$file_count ++;
last;
}
}
}else{
$file_count ++;
}
}

print "file count = $file_count\n";


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


DoolinDalton
Novice

Sep 25, 2009, 11:59 AM

Post #4 of 4 (3285 views)
Re: [shawnhcorey] glob and regex [In reply to] Can't Post

Thank you for the ideas.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives