Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

greping for names

by Sara (Acolyte)
on Jul 19, 2002 at 14:16 UTC ( [id://183237]=perlquestion: print w/replies, xml ) Need Help??

Sara has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks, I am parsing a file that contains names of different files ,, and want only the .c guys ,, my file has
one.cpp sdee.c messf.pl info cared.java morse.c dosss.h
I am doing this :
while (<>) { if ($theCfile eq "\.c\$") { print $_; }
but not getting the right ones .. thanks for help or ideas

Replies are listed 'Best First'.
Re: greping for names
by bronto (Priest) on Jul 19, 2002 at 14:23 UTC

    use strict, Sara.

    Why are you using $theCgfile when your cycle assigns to $_?

    If I get what you need, you should actually write:

    use strict ; use warnings ; while (<>) { print if /\.c$/ ; }

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Re: greping for names
by moxliukas (Curate) on Jul 19, 2002 at 14:21 UTC

    You should use a regular expression like this:

    while (<>) { print if /\.c$/; }
Re: greping for names
by Cine (Friar) on Jul 19, 2002 at 14:48 UTC
    Since you mention grep yourself:
    print grep {/\.c$/} <>;


    T I M T O W T D I
Re: greping for names
by sschneid (Deacon) on Jul 19, 2002 at 14:23 UTC
    Your regex seems to be a bit messed up...
    while (<>) { if ($theCfile =~ /\.c$/) { print $_ } }
    Matching on regex uses the =~ operator, not eq, and $ anchros it to the end of the line, so that one.cpp will not be included.

    Hope it helps.

    scott.
Re: greping for names
by Nightblade (Beadle) on Jul 19, 2002 at 14:31 UTC
    IMHO substr() works faster than regexp:
    while(<>) { print if(substr($_,-3,2) eq ".c"); }

      However IMHO this approach is more fragile. I am not sure how this would work on different platforms, because new line is two characters wide on some platforms and only one character wide on the others.

      I might be picky, but if you are really going for speed, use single qoutes istead of double (I know in reality it makes almost no difference, and I'm just being picky ;)

        For the record, ".c" will get optimized to '.c', so it really doesn't matter. Consider
        % perl -MO=Deparse -e 'print ".c";' -e syntax OK print '.c';

        Cheers,
        Shendal
Re: greping for names
by snafu (Chaplain) on Jul 19, 2002 at 15:33 UTC
    I believe this is a job for superman...glob().
    my @cfiles = glob('*.c'); print join("\n",@cfiles),"\n";
    And here it is in action:

    $ ls test_ test_data_rw.C test_exitcode test_exitcode.c test_rw_bindata +.c $ perl -e ' >my @cfiles = glob("*.c"); # used "'s for CLI input. >print join("\n",@cfiles),"\n"; >' test_exitcode.c test_rw_bindata.c

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183237]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-29 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found