Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

glob an array of filenames

Status
Not open for further replies.

NullTerminator

Programmer
Oct 13, 1998
293
US
I want to glob an array of filenames retrieved from a source control list command.

The array contains a list of filenames with newlines.
I could walk the list and pre chomp them, but how to feed them to glob and get the filtered list out

Tnx
Jeb
\0
 
This post has been here long enough. Here we go!

This takes the contents of a directory and adds each * to an array.
Code:
#!/usr/bin/perl -w

use strict;

use FileHandle;

my $dir = "/path/to/directory";

opendir DIR, "$dir/" or die "Failed to open $!";
my @files = readdir(DIR);
closedir DIR;

my @glob_array;

my $i = 0;
for my $file (@files) {
    unless ( $file eq '.' || $file eq '..' ) {
        $glob_array[$i] = ( new FileHandle("<$file") );
        $i++;
    }
}

my $fh1 = readline($glob_array[0]);

print $fh1 . "\n";

M. Brooks
 
Thanks MBrooks. Not exactly what I'm trying to do.
Subvrsion (source control) list command returns a list of filenames separated by newline.

I want to apply a user supplied wildcard pattern to the array of names returned

code.java
proj.properties
readme.txt

user supplies "*.java"

routine returns
[ code.java ]

I did a workaround by trnaslating the the * and ? filepatterns to regex .* and ." and then walking the list.

I thought there might be a way to use the pattern matching portion of glob against a array of names.
 
maybe:

Code:
$user = '*.java';
$user =~ s/*\.//;
chdir('/path/to/files/') or die "$!";
@files = <*\.$user>;


 
sorry, there no concept of chdir in the picture. Calling ls on the source control product returns a flat list of unqualified file and directory names. An array of lines is returned from `svn ls
I want to provide a glob like filtering for the end users. Can I feed an array of names to glob.

I worked around it like this
Code:
sub globNames {
    my ($pattern, @list) = @_;
    my @listOut;
    # replace shell wildcards with perl style
    $pattern =~ s/\*/.*/;
    $pattern =~ s/\?/.?/;
    $pattern =~ s/\\/\//;
    
    for (@list) {
        if( /$pattern/ ) {
            push (@listOut, $_ );
        }
    }
    return @listOut;    
}   # end globNames

I like kevins trick with
Code:
 @files = <*\.$user>;
but I'll have to look into building more flexibility into it.

Thanks guys
Jeb\0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top