Need help with grep function

Discussion in 'Perl Misc' started by perlUSER, Apr 28, 2006.

  1. perlUSER

    perlUSER Guest

    Hello;
    I am trying to select files based on the pattern and can't get the
    script to work on one of the pattern. Code snippet:

    my @list = glob('*');
    @list = grep {m[^EOM_[A-Z][a-z]{2}_\d{4}]} @list;
    foreach my $list (@list){
    print $list."\n";
    }
    The above code return all files matching the pattern. Ex: EOM_Jan_2006,
    EOM_Feb_2006.

    my @list = glob('*');
    @list = grep {! m[^\d{2}_d{2}_d{2}]} @list;
    foreach my $list (@list){
    print $list."\n";
    }
    I am expecting to see files like these 06_04_01, 06_04_02 thru
    06_04_28.

    Please help me with this issue.

    Thanks in advance.
     
    perlUSER, Apr 28, 2006
    #1
    1. Advertising

  2. perlUSER

    Paul Lalli Guest

    perlUSER wrote:
    > my @list = glob('*');
    > @list = grep {! m[^\d{2}_d{2}_d{2}]} @list;
    > foreach my $list (@list){
    > print $list."\n";
    > }
    > I am expecting to see files like these 06_04_01, 06_04_02 thru
    > 06_04_28.


    Then why are you writing code to specifically return all files that do
    *not* match that pattern?

    What do you think that ! is doing in the grep block?

    Paul Lalli
     
    Paul Lalli, Apr 28, 2006
    #2
    1. Advertising

  3. Paul Lalli wrote:
    > perlUSER wrote:
    > > my @list = glob('*');
    > > @list = grep {! m[^\d{2}_d{2}_d{2}]} @list;
    > > foreach my $list (@list){
    > > print $list."\n";
    > > }
    > > I am expecting to see files like these 06_04_01, 06_04_02 thru
    > > 06_04_28.

    >
    > Then why are you writing code to specifically return all files that do
    > *not* match that pattern?
    >
    > What do you think that ! is doing in the grep block?


    marking it as "High Importance".
     
    it_says_BALLS_on_your forehead, Apr 28, 2006
    #3
  4. perlUSER

    perlUSER Guest

    Paul Lalli wrote:
    > perlUSER wrote:
    > > my @list = glob('*');
    > > @list = grep {! m[^\d{2}_d{2}_d{2}]} @list;
    > > foreach my $list (@list){
    > > print $list."\n";
    > > }
    > > I am expecting to see files like these 06_04_01, 06_04_02 thru
    > > 06_04_28.

    >
    > Then why are you writing code to specifically return all files that do
    > *not* match that pattern?
    >
    > What do you think that ! is doing in the grep block?
    >
    > Paul Lalli


    Paul,
    Thanks for your response. Actually I was testing various scenarious and
    forgot to remove the '!' from my code before I posted it here.
    To remove any confusion, I copied the hole script here:

    #!/usr/bin/perl -w

    my @list = glob('*');
    @list = grep {m[^\d{2}_d{2}_d{2}]} @list;

    foreach my $list (@list){
    print $list."\n";
    }

    This script didn't return any even though I have files matching that
    pattern.

    And here is my directory listing where my test.pl is:
    $ ls
    02_02_02

    Any suggestions what I am doing wrong?

    Thanks in advance.
     
    perlUSER, Apr 28, 2006
    #4
  5. perlUSER

    Ingo Menger Guest

    perlUSER schrieb:

    > Paul Lalli wrote:
    > > perlUSER wrote:
    > > > my @list = glob('*');
    > > > @list = grep {! m[^\d{2}_d{2}_d{2}]} @list;
    > > > foreach my $list (@list){
    > > > print $list."\n";
    > > > }
    > > > I am expecting to see files like these 06_04_01, 06_04_02 thru
    > > > 06_04_28.

    > >
    > > Then why are you writing code to specifically return all files that do
    > > *not* match that pattern?
    > >
    > > What do you think that ! is doing in the grep block?
    > >
    > > Paul Lalli

    >
    > Paul,
    > Thanks for your response. Actually I was testing various scenarious and
    > forgot to remove the '!' from my code before I posted it here.
    > To remove any confusion, I copied the hole script here:
    >
    > #!/usr/bin/perl -w
    >
    > my @list = glob('*');
    > @list = grep {m[^\d{2}_d{2}_d{2}]} @list;
    >
    > foreach my $list (@list){
    > print $list."\n";
    > }
    >
    > This script didn't return any even though I have files matching that
    > pattern.
    >
    > And here is my directory listing where my test.pl is:
    > $ ls
    > 02_02_02
    >
    > Any suggestions what I am doing wrong?


    Yes. Please, do the following:
    - run command the following shell command: touch 42_dd_dd
    - repeat your script.
     
    Ingo Menger, Apr 28, 2006
    #5
  6. perlUSER

    jck11 Guest

    "perlUSER" <>
    ???????:...
    >
    > Paul Lalli wrote:
    >> perlUSER wrote:
    >> > my @list = glob('*');
    >> > @list = grep {! m[^\d{2}_d{2}_d{2}]} @list;
    >> > foreach my $list (@list){
    >> > print $list."\n";
    >> > }
    >> > I am expecting to see files like these 06_04_01, 06_04_02 thru
    >> > 06_04_28.

    >>
    >> Then why are you writing code to specifically return all files that do
    >> *not* match that pattern?
    >>
    >> What do you think that ! is doing in the grep block?
    >>
    >> Paul Lalli

    >
    > Paul,
    > Thanks for your response. Actually I was testing various scenarious and
    > forgot to remove the '!' from my code before I posted it here.
    > To remove any confusion, I copied the hole script here:
    >
    > #!/usr/bin/perl -w
    >
    > my @list = glob('*');
    > @list = grep {m[^\d{2}_d{2}_d{2}]} @list;


    @list = grep {m[^\d{2}_\d{2}_\d{2}]} @list;

    >
    > foreach my $list (@list){
    > print $list."\n";
    > }
    >
    > This script didn't return any even though I have files matching that
    > pattern.
    >
    > And here is my directory listing where my test.pl is:
    > $ ls
    > 02_02_02
    >
    > Any suggestions what I am doing wrong?
    >
    > Thanks in advance.
    >
     
    jck11, Apr 28, 2006
    #6
  7. perlUSER

    perlUSER Guest


    > > Any suggestions what I am doing wrong?

    >
    > Yes. Please, do the following:
    > - run command the following shell command: touch 42_dd_dd
    > - repeat your script.


    You are funny. Your solution will work with out a question. Thanks for
    the comment.
     
    perlUSER, Apr 28, 2006
    #7
  8. perlUSER

    perlUSER Guest

    jck11 wrote:
    >
    > @list = grep {m[^\d{2}_\d{2}_\d{2}]} @list;
    >
    > >



    This worked. I guess I forgot to place the '\' in the grep.

    Thank you very much.
     
    perlUSER, Apr 28, 2006
    #8
  9. perlUSER wrote:
    > jck11 wrote:
    > >
    > > @list = grep {m[^\d{2}_\d{2}_\d{2}]} @list;
    > >
    > > >

    >
    >
    > This worked. I guess I forgot to place the '\' in the grep.


    That's what Ingo Menger was telling you, in an oblique sort of way.
     
    it_says_BALLS_on_your forehead, Apr 28, 2006
    #9
  10. perlUSER

    Tintin Guest

    "perlUSER" <> wrote in message
    news:...
    >
    > > > Any suggestions what I am doing wrong?

    > >
    > > Yes. Please, do the following:
    > > - run command the following shell command: touch 42_dd_dd
    > > - repeat your script.

    >
    > You are funny. Your solution will work with out a question. Thanks for
    > the comment.


    Did you try what Ingo suggested? It was meant as a gentle nudge to help you
    detect your problem.
    >
     
    Tintin, Apr 29, 2006
    #10
  11. perlUSER

    perlUSER Guest

    Tintin wrote:
    > "perlUSER" <> wrote in message
    > news:...
    > >
    > > > > Any suggestions what I am doing wrong?
    > > >
    > > > Yes. Please, do the following:
    > > > - run command the following shell command: touch 42_dd_dd
    > > > - repeat your script.

    > >
    > > You are funny. Your solution will work with out a question. Thanks for
    > > the comment.

    >
    > Did you try what Ingo suggested? It was meant as a gentle nudge to help you
    > detect your problem.
    > >


    Tintin,
    I din't try what Ingo suggested, as soon as I saw his post, I figured
    out what the problem is, thats why I said he is funny. Instead of
    giving the solution he told me what to do in order to make my script.

    Thanks.
     
    perlUSER, Apr 29, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danpres2k
    Replies:
    3
    Views:
    7,519
    danpres2k
    Aug 25, 2003
  2. John
    Replies:
    4
    Views:
    148
    Ben Morrow
    Nov 2, 2004
  3. Mark
    Replies:
    3
    Views:
    126
    Tad McClellan
    Jul 7, 2005
  4. perlmbk
    Replies:
    3
    Views:
    587
  5. Deepu

    Help with grep function

    Deepu, Jul 11, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    143
    Xicheng Jia
    Jul 11, 2006
Loading...

Share This Page