in reply to Re^3: Reading Directory and getting pattern from a file
in thread Reading Directory and getting pattern from a file

Sorry, I was not aware of your name. Your tag line is cool. :) So far I have written this much of code. The power directory has two directories and in those directories. There are 130 sub directories for each directories. I want to go to each sub directory and get a pattern from a text file in that directory. I am stuck with how to check directories and get that perticular pattern. mdp is a pattern. Your help will be greatly appreciated.

#!usr/bin/perl; use strict; use warnings; $| = 1; sub main{ my $directory = '/nfs/fm/stod/stod1031/w.eakanoje.101/power/'; opendir (DIR, $directory) or die "Cannot open directory $directory" +; # while (my $file = readdir(DIR)){ # next if($file =~ m/^\./); # print "$file\n"; # } my @out = glob("mdp*"); print "@out\n"; } main();
------------------------------------------

Replies are listed 'Best First'.
Re^5: Reading Directory and getting pattern from a file
by SuicideJunkie (Vicar) on Feb 10, 2015 at 21:06 UTC

    Note: Do not include line numbers in your code posts. It makes them not compile.

    Go to your Profile->{settings}->{display settings}->{code Prefix} and set it to something like "&001;: "; That will give you line numbers when viewing on perlmonks without breaking the actual code.

Re^5: Reading Directory and getting pattern from a file
by soonix (Chancellor) on Feb 11, 2015 at 11:27 UTC
    by
    get a pattern from a text file
    do you mean
    • list only file names that match a pattern
    • read a pattern from a file's contents?
    "get a pattern" sounds like the latter, but your example looks more like the former.

    I like Path::Tiny for such tasks, e.g. the following would show you all files in the tree where the name starts with "mdp". Note this doesn't use shell patterns like glob (with * and ?) but perl's own regexes.

    use strict; use warnings; use Path::Tiny qw/path/; for ( path('/nfs/fm/stod/stod1031/w.eakanoje.101/power/')->children( q +r/^mdp/ ) ) { print $_ . $/; }
      Thanks a lot soonix.