Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Can figure out a way to get File::Grep's fgrep to chomp matches

by LanX (Archbishop)
on May 13, 2017 at 23:45 UTC ( #1190242=note: print w/replies, xml ) Need Help??


in reply to Can figure out a way to get File::Grep's fgrep to chomp matches

I did a text search for chomp in File::Grep and found this example in the synopsis

 fmap { chomp; lc; }  

hence there is no other way than doing the obvious

my @line = map { chomp }  fgrep { /^[2-6] .+$/ } *DATA;

This should do too

my @line = grep { /^[2-6] .+$/ } fmap {chomp} *DATA;

HTH

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

update

The usual trick to include the grep condition into map is left as exercise. ;)

Replies are listed 'Best First'.
Re^2: Can figure out a way to get File::Grep's fgrep to chomp matches
by Lady_Aleena (Curate) on May 14, 2017 at 00:01 UTC

    Thanks for trying, but here are the results of both directions:

    my @line = map { chomp }  fgrep { /^[2-6] .+$/ } *DATA;

    $VAR1 = [ 0 ];

    my @line = grep { /^[2-6] .+$/ } fmap {chomp} *DATA;

    $VAR1 = [];

    It is okay if it is not possible until later munging. It is just a tad annoying.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena

      chomp doesn’t return quite like that. This should work–

      my @line = map { chomp; $_ } fgrep { /^[2-6] .+$/ } *DATA;

      But maybe this is better–

      chomp( my @line = fgrep { /^[2-6] .+$/ } *DATA );

        Here is what your suggestion returned...

        $VAR1 = [ { 'count' => 5, 'matches' => { '3' => '4 baz ', '5' => '6 quux ', '2' => '3 bar ', '4' => '5 qux ', '1' => '2 foo ' }, 'filename' => *::DATA } ];

        I am beginning to believe this module is not set up for chomping the matches until they are munged later. The reason I even started this was that the dumper data looks bad with the newlines in the data.

        No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
        Lady Aleena
      I see, please try

        my @line = fmap { chomp; /^[2-6] .+$/ ? $_ : () } *DATA;  

      Sorry, I don't think this interface was well designed ...

      HTH :)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        It worked, however, it would not help when feeling it a list of files where one wants the line numbers and file names.

        $VAR1 = [ '2 foo', '3 bar', '4 baz', '5 qux', '6 quux' ];

        If I feed it a list of files, I get a list of everything which matches but no line numbers and file names attached. You helped a lot today! Thank you!

        No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
        Lady Aleena

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190242]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2019-09-15 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (181 votes). Check out past polls.

    Notices?