Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Using a regex function from a hash

by friedo (Prior)
on Aug 02, 2006 at 18:49 UTC ( #565293=note: print w/replies, xml ) Need Help??


in reply to Using a regex function from a hash

You could wrap the substitution in a code reference:

my %functions_hash = ( '\.vms' => sub { $_[0] =~ s{/}{} } ); ... if ( $data =~ /$key/ ) { $value->( $data ); }

Replies are listed 'Best First'.
Re^2: Using a regex function from a hash
by perlNinny (Beadle) on Aug 03, 2006 at 17:23 UTC
    EXCELLENT. now a follow up. I am trying to get the word out of a line without much success. Here is some example lines:

    Created by GeorgeO at the parker inn
    date : 06-Aug-06:22.34.01 buncahstuffIdon'twant
    Created by 7of9 at the parker inn
    date : 06-Jan-06:22.34.01 buncahstuffIdon'twant
    Please get me:
    GeorgeO
    06-Aug-06:22.34.01
    7of9
    06-Jan-06:22.34.01

    my %functions_hash = ( '^Created by ' => sub { $_[0] =~ /^Created by (\w+)/; $_[0] = $1; }, '^date :' => sub ( $_[0] =~ /^data :(#HELP!!)/; $_[0] = $1 ); ... if ( $data =~ /$key/ ) { $value->( $data ); }
      Messed up my question on the regex thing. Here is some example lines:

      Created by a.buncha.stuff.I.dont.want\GeorgeO at the parker inn
      date : 06-Aug-06:22.34.01 buncahstuffIdon'twant
      Created by a.buncha.stuff.I.dont.want\7of9 at the parker inn
      date : 06-Jan-06:22.34.01 buncahstuffIdon'twant

      Please get me:
      GeorgeO
      06-Aug-06:22.34.01
      7of9
      06-Jan-06:22.34.01

      2006-08-03 Moved and retitled by GrandFather, as per Monastery guidelines
      Original title: 'Regex, extracting stuff from the middle of a line'

        Your requirements are very vague.

        • You didn't specify which components are constant and which are not. I assumed "\" and "date : " are constant.
        • You didn't specify of which chars the variable parts could be made. I assumed what is represented by "GeorgeO" could contain spaces (which required specifying " at the parker inn"),
        • You didn't specufy from where the lines came. I assumed a file handle.
        • You didn't specify what to do with the extracted values. I print them.
        while (<$fh>) { print("$1\n") if /\\(.*?) at the parker inn$/ or /^date : (\S+)/; }

        And your code that's not working looks like . . .?

        Show some effort of your own before submitting work requests. See How (Not) To Ask A Question.

        Update: Oop, didn't realize this was a followup to a prior question.

        Probably because it was exceedingly vague and offered no context or indication otherwise, so the how-not-to-ask-ing's still relevant.

        You should escape the special '\' symbol:
        m|\\(\w+)| and print $1, "\n";
        For the date/time:
        m|:\s+(\S+)| and print $1, "\n";
        See perlre


        While ikegami said pretty much everything I wanted to say, I suggest that if you can come up with a set of explicit rules for what you want removed or, conversely, kept, the code will be easy (perhaps not the most elegant possible).

        emc

        Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.

        Groucho Marx

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-09-25 14:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?