Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need help with grep function

Reply
Thread Tools

Need help with grep function

 
 
perlUSER
Guest
Posts: n/a
 
      04-28-2006
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.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      04-28-2006
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

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      04-28-2006

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".

 
Reply With Quote
 
perlUSER
Guest
Posts: n/a
 
      04-28-2006

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.

 
Reply With Quote
 
Ingo Menger
Guest
Posts: n/a
 
      04-28-2006

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.

 
Reply With Quote
 
jck11
Guest
Posts: n/a
 
      04-28-2006

"perlUSER" <(E-Mail Removed)>
???????:(E-Mail Removed) groups.com...
>
> 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.
>



 
Reply With Quote
 
perlUSER
Guest
Posts: n/a
 
      04-28-2006

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

 
Reply With Quote
 
perlUSER
Guest
Posts: n/a
 
      04-28-2006

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.

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      04-28-2006

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.

 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      04-29-2006

"perlUSER" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> > > 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need grep Don Culp Windows 64bit 8 05-16-2008 03:02 AM
Help with grep function Deepu Perl Misc 5 07-11-2006 10:42 PM
Want to use equivalent of grep function in perl perlmbk Perl Misc 3 11-10-2005 12:20 PM
Should I write my own Grep or Index function ? Mark Perl Misc 3 07-07-2005 12:32 PM
writing a function like map, sort, grep John Perl Misc 4 11-02-2004 11:17 PM



Advertisments