[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regex quickie



On Apr 26, 2007, at 9:15 AM, Andy Armstrong wrote:
    my %despatch = (
        'yahoo'  => sub { print "Yahoo!\n"; },
        'google' => sub { print "Google!\n"; },
    );

    my $match = join('|', map {quotemeta} keys %despatch);
    my $re = qr/($match)/i;       # or whatever

    print "Using: $re\n";
    for my $t ('I like yahoo', 'and also google') {
        $despatch{lc($1)}->() if $t =~ $re;
    }

completely off topic....

i'd keep the dispatch essentially the same, but change the regex to something more like this:

	my %despatch = (
	  'yahoo' => sub { print "\n Yahoo! $_[0]\n"; },
	  'google' => sub { print "\n Google! $_[0]\n"; },
	 );

	sub dispatcher
	{
		my	( $string )= @_;
my @parts= $string =~ qr/^(?:http:\/\/)?(?:www\.)?(google|yahoo|msn) \.com(?:\/\?q=([^&]*))?/i;
		if ( scalar @parts ) {
			my	( $domain , $q )= @parts;
			$despatch{lc($domain)}->($q);
		}
	}

	dispatcher( 'google.com' );
	dispatcher( 'google.com/?q=test' );


still very dirtry -- i prefer putting refs in the dispatch, or class method names. but that shows you a quick way to catch the q var and pass it to your function using Andy's example :)



// Jonathan Vanasco

| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| FindMeOn.com - The cure for Multiple Web Personality Disorder
| Web Identity Management and 3D Social Networking
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| RoadSound.com - Tools For Bands, Stuff For Fans
| Collaborative Online Management And Syndication Tools
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.