Pattern Matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sadanand
    New Member
    • May 2007
    • 6

    Pattern Matching

    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,89 0 files in the following folder.
    C:\perl_practic e\Folder

    Code:-

    [CODE=perl]
    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,$cb r);
    @files = readdir(DIR);
    closedir(DIR);
    foreach $file (sort @files) {
    print("$file \n");
    }
    }
    [/CODE]

    When I execute this program.All the files available in the C:\perl_practic e\Folder are diplayed.

    Code:
    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

    Code:
    ICON_SIM
    MYCO_SIM
    SAME_SIM
    Waiting for ur valuable response

    Regards,
    Sada
    Last edited by miller; May 22 '07, 04:40 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    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.

    [CODE=perl]
    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$/;
    }
    }
    [/CODE]

    - Miller

    Comment

    • prn
      Recognized Expert Contributor
      • Apr 2007
      • 254

      #3
      Another plausible idiom:
      [code=perl]
      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";
      }
      }
      [/code]
      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

      Comment

      • prn
        Recognized Expert Contributor
        • Apr 2007
        • 254

        #4
        OBTW, you (sada) initially said:
        Originally posted by sada
        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

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          in the spirit of TIMTOWTDI

          use strict;
          my $pattern = "SIM";
          my $test_data1='C:/perl_practice/Folder/';
          print "$_\n" for sort <$test_data1*$p attern>;

          Comment

          • prn
            Recognized Expert Contributor
            • Apr 2007
            • 254

            #6
            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

            Comment

            Working...