using file globs and regex

Discussion in 'Perl Misc' started by pgodfrin, Aug 7, 2009.

  1. pgodfrin

    pgodfrin Guest

    Greetings,
    This program works fine (with file names f1..f8 in the directory):

    #!/usr/bin/perl
    use warnings;
    use strict;
    $\="\n";
    while(<f*>)
    {
    print "$_";
    if(/f5/)
    {
    my $fn=$_;
    print "File name $fn found..." and exit;
    }
    }
    exit;

    But I was hoping to be able to do something like:
    my $fn=grep (/f5/,<f*>);

    Basically just trying to have less lines of code...any suggestions?
    phil g
    pgodfrin, Aug 7, 2009
    #1
    1. Advertising

  2. pgodfrin

    pgodfrin Guest

    Thanks Tad - works like a charm...
    pg

    On Aug 7, 12:06 pm, Tad J McClellan <> wrote:
    > pgodfrin <> wrote:
    > > Greetings,
    > > This program works fine (with file names f1..f8 in the directory):

    >
    > > #!/usr/bin/perl
    > > use warnings;
    > > use strict;
    > > $\="\n";
    > > while(<f*>)
    > > {
    > >     print "$_";
    > >     if(/f5/)
    > >     {
    > >         my $fn=$_;
    > >         print "File name $fn found..." and exit;
    > >     }
    > > }
    > > exit;

    >
    > > But I was hoping to be able to do something like:
    > > my $fn=grep (/f5/,<f*>);

    >
    > > Basically just trying to have less lines of code...any suggestions?

    >
    > use grep() in a list context instead of in a scalar context.
    >
    > I don't like using overloaded angle brackets in my code.
    >
    > If I want equality, I use an operator that tests for equality.
    >
    >     my($fn) = grep ( $_ eq 'f5' , glob 'f*');
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    pgodfrin, Aug 7, 2009
    #2
    1. Advertising

  3. pgodfrin

    pgodfrin Guest

    hmmm - what if I wanted to do some search on the glob?
    like my($fn) = grep ( /5/ , glob 'f*');
    ?
    pg
    On Aug 7, 12:23 pm, pgodfrin <> wrote:
    > Thanks Tad - works like a charm...
    > pg
    >
    > On Aug 7, 12:06 pm, Tad J McClellan <> wrote:
    >
    > > pgodfrin <> wrote:
    > > > Greetings,
    > > > This program works fine (with file names f1..f8 in the directory):

    >
    > > > #!/usr/bin/perl
    > > > use warnings;
    > > > use strict;
    > > > $\="\n";
    > > > while(<f*>)
    > > > {
    > > >     print "$_";
    > > >     if(/f5/)
    > > >     {
    > > >         my $fn=$_;
    > > >         print "File name $fn found..." and exit;
    > > >     }
    > > > }
    > > > exit;

    >
    > > > But I was hoping to be able to do something like:
    > > > my $fn=grep (/f5/,<f*>);

    >
    > > > Basically just trying to have less lines of code...any suggestions?

    >
    > > use grep() in a list context instead of in a scalar context.

    >
    > > I don't like using overloaded angle brackets in my code.

    >
    > > If I want equality, I use an operator that tests for equality.

    >
    > >     my($fn) = grep ( $_ eq 'f5' , glob 'f*');

    >
    > > --
    > > Tad McClellan
    > > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >
    >
    pgodfrin, Aug 7, 2009
    #3
  4. pgodfrin

    pgodfrin Guest

    OK - I figured it out - thanks.

    This works fine: my($fn2) = grep ( $_ =~ '6' , glob 'f*');

    What I'm confused about is the parens around the $fn2 ? Why are those
    needed? Something tells me that has something to do with making the
    scalar $fn2 into a list 'cause that's what grep returns, but i can't
    remember what that's called to look it up.

    help?
    pg


    On Aug 7, 1:23 pm, pgodfrin <> wrote:
    > hmmm - what if I wanted to do some search on the glob?
    > like my($fn) = grep ( /5/ , glob 'f*');
    > ?
    > pg
    > On Aug 7, 12:23 pm, pgodfrin <> wrote:
    >
    > > Thanks Tad - works like a charm...
    > > pg

    >
    > > On Aug 7, 12:06 pm, Tad J McClellan <> wrote:

    >
    > > > pgodfrin <> wrote:
    > > > > Greetings,
    > > > > This program works fine (with file names f1..f8 in the directory):

    >
    > > > > #!/usr/bin/perl
    > > > > use warnings;
    > > > > use strict;
    > > > > $\="\n";
    > > > > while(<f*>)
    > > > > {
    > > > >     print "$_";
    > > > >     if(/f5/)
    > > > >     {
    > > > >         my $fn=$_;
    > > > >         print "File name $fn found..." and exit;
    > > > >     }
    > > > > }
    > > > > exit;

    >
    > > > > But I was hoping to be able to do something like:
    > > > > my $fn=grep (/f5/,<f*>);

    >
    > > > > Basically just trying to have less lines of code...any suggestions?

    >
    > > > use grep() in a list context instead of in a scalar context.

    >
    > > > I don't like using overloaded angle brackets in my code.

    >
    > > > If I want equality, I use an operator that tests for equality.

    >
    > > >     my($fn) = grep ( $_ eq 'f5' , glob 'f*');

    >
    > > > --
    > > > Tad McClellan
    > > > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >
    >
    pgodfrin, Aug 7, 2009
    #4
  5. pgodfrin

    Steve C Guest

    - SC.

    then it asks grep for a scalar.
    but when you call it without the parens
    it asks grep for a list
    When you put $fn2 inside the parens then
    It's called list context.

    pgodfrin wrote:
    > OK - I figured it out - thanks.
    >
    > This works fine: my($fn2) = grep ( $_ =~ '6' , glob 'f*');
    >
    > What I'm confused about is the parens around the $fn2 ? Why are those
    > needed? Something tells me that has something to do with making the
    > scalar $fn2 into a list 'cause that's what grep returns, but i can't
    > remember what that's called to look it up.
    >
    > help?
    > pg
    >
    >
    > On Aug 7, 1:23 pm, pgodfrin <> wrote:
    >> hmmm - what if I wanted to do some search on the glob?
    >> like my($fn) = grep ( /5/ , glob 'f*');
    >> ?
    >> pg
    >> On Aug 7, 12:23 pm, pgodfrin <> wrote:
    >>
    >>> Thanks Tad - works like a charm...
    >>> pg
    >>> On Aug 7, 12:06 pm, Tad J McClellan <> wrote:
    >>>> pgodfrin <> wrote:
    >>>>> Greetings,
    >>>>> This program works fine (with file names f1..f8 in the directory):
    >>>>> #!/usr/bin/perl
    >>>>> use warnings;
    >>>>> use strict;
    >>>>> $\="\n";
    >>>>> while(<f*>)
    >>>>> {
    >>>>> print "$_";
    >>>>> if(/f5/)
    >>>>> {
    >>>>> my $fn=$_;
    >>>>> print "File name $fn found..." and exit;
    >>>>> }
    >>>>> }
    >>>>> exit;
    >>>>> But I was hoping to be able to do something like:
    >>>>> my $fn=grep (/f5/,<f*>);
    >>>>> Basically just trying to have less lines of code...any suggestions?
    >>>> use grep() in a list context instead of in a scalar context.
    >>>> I don't like using overloaded angle brackets in my code.
    >>>> If I want equality, I use an operator that tests for equality.
    >>>> my($fn) = grep ( $_ eq 'f5' , glob 'f*');
    >>>> --
    >>>> Tad McClellan
    >>>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >>

    >
    Steve C, Aug 7, 2009
    #5
  6. Tad J McClellan wrote:
    > pgodfrin <> wrote:
    >> Greetings,
    >> This program works fine (with file names f1..f8 in the directory):
    >>
    >> #!/usr/bin/perl
    >> use warnings;
    >> use strict;
    >> $\="\n";
    >> while(<f*>)
    >> {
    >> print "$_";
    >> if(/f5/)
    >> {
    >> my $fn=$_;
    >> print "File name $fn found..." and exit;
    >> }
    >> }
    >> exit;
    >>
    >> But I was hoping to be able to do something like:
    >> my $fn=grep (/f5/,<f*>);
    >>
    >> Basically just trying to have less lines of code...any suggestions?

    >
    >
    > use grep() in a list context instead of in a scalar context.
    >
    > I don't like using overloaded angle brackets in my code.
    >
    > If I want equality, I use an operator that tests for equality.
    >
    > my($fn) = grep ( $_ eq 'f5' , glob 'f*');


    Or just don't remove the equality in the first place.

    my($fn) = glob 'f5';

    Although it seems like a file test operator would make more sense here.

    Xho
    Xho Jingleheimerschmidt, Aug 8, 2009
    #6
  7. pgodfrin

    pgodfrin Guest

    On Aug 7, 3:44 pm, Ben Morrow <> wrote:
    > [please don't top-post]
    >
    > Quoth pgodfrin <>:
    >
    > > OK - I figured it out - thanks.

    >
    > > This works fine: my($fn2) = grep ( $_ =~ '6' , glob 'f*');

    >
    > > What I'm confused about is the parens around the $fn2 ? Why are those
    > > needed? Something tells me that has something to do with making the
    > > scalar $fn2 into a list 'cause that's what grep returns, but i can't
    > > remember what that's called to look it up.

    >
    >     my $x = ...
    >
    > evaluates the RHS in scalar context, but
    >
    >     my ($x) = ...
    >
    > evaluates it in list context. The same applies without the 'my', or with
    > 'local' instead. This is one of the few cases where explicit parens make
    > a difference in deciding whether something is a list or not.
    >
    > Ben


    Nice to hear from you Ben (hope things are well).
    Thanks Ben and Tad for clearing up the 'context'.
    Of course, you're right Tad - the code assumes the first one it finds
    is the right one. In this case that should be ok - but knowing more
    about that behavior is a good thing (and might keep me from posting
    some other day).

    (sorry about the top post)
    pg
    pgodfrin, Aug 10, 2009
    #7
  8. pgodfrin

    Steve C Guest

    Ben Morrow wrote:
    > [please don't top-post]
    >
    > Quoth Steve C <>:
    >> - SC.
    >>
    >> then it asks grep for a scalar.
    >> but when you call it without the parens
    >> it asks grep for a list

    >
    > Please don't talk nonsense.
    >
    > Ben
    >


    Too subtle, I guess.
    SC
    Steve C, Aug 10, 2009
    #8
    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. Replies:
    3
    Views:
    734
    Reedick, Andrew
    Jul 1, 2008
  2. Alex Hart
    Replies:
    0
    Views:
    80
    Alex Hart
    Jan 24, 2005
  3. Alex Hart
    Replies:
    1
    Views:
    95
    Anno Siegel
    Jan 24, 2005
  4. Replies:
    2
    Views:
    384
  5. Tim Watts

    MasonCompRoot and globs

    Tim Watts, Jul 15, 2010, in forum: Perl Misc
    Replies:
    6
    Views:
    130
    Tim Watts
    Jul 29, 2010
Loading...

Share This Page