Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: How to grep for a filename for each of the keys of a hash

by Anonymous Monk
on Dec 13, 2010 at 04:47 UTC ( #876789=note: print w/replies, xml ) Need Help??

in reply to How to grep for a filename for each of the keys of a hash

1: $filename is presumably a string, not a pattern, which you want to match; so you have to escape metacharacters first if you're going to use it in a regular expression: /\Q$filename\E/, for example, if you don't want to modify that variable with the quotemeta function or don't need the quoted version for anything else ($foo = quotemeta $foo; or my $foo_quoted = quotemeta $foo;</code>).


  • my ($foo) = $depopath =~ /\s(\S+)$/;
  • or my ($foo) = reverse split ' ', $depopath;

Replies are listed 'Best First'.
Re^2: How to grep for a filename for each of the keys of a hash
by k_manimuthu (Monk) on Dec 13, 2010 at 05:43 UTC

    As per above comments you use the quotemeta function.
    Insist of the below code

    grep (/$filename/i, $key) foreach my $key (keys %path_versions)

    You will try the below code. Here, you no need to use the foreach loop. You directly pass 'hash keys'.

    @array = grep (/$filename/, keys %path_versions);
      push @array = grep (/\Q$file_name\E/i, keys %pathname_versions);

      I am getting the below error when the above piece of code is executed,what does it mean?

      Type of arg 1 to push must be array (not list assignment)

        my @array = grep (/\Q$file_name\E/i, keys %pathname_versions);

        That's the right thing.

        s/ =/,/ in that code.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2016-10-23 10:53 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (301 votes). Check out past polls.