This begs for the use of an 'ini' file and to exploit the lookup abilities of a hash.

As it appears that the url is embedded in other stuff, and you appear to want to root pathed urls to the root of the redirects--Is that what the \W => \n stuff is doing?--then you can't use a straight lookup in a hash.

You first need to separate out the domain name for the lookup, and substitute that back into the original string.

#! perl -slw use strict; use Data::Dumper; my %map = map{ split /=/, $_, 2 } split /\n/, do{ local $/; <DATA> }; print Dumper \%map; while( <> ) { chomp; if( m[http://([^/]+)/(\W?)]i ) { next unless defined $map{ $1 }; substr( $_, $-[0], $+[0] - $-[0] ) = 'http://' . $map{ $1 } . ( length $2 ? $2 : "\n$2" ); } print; } __DATA__ www.theirsite.com=our.server1/theirsite/ www.dummy.com=our.server2/dummy/
P:\test>type junk.txt http://www.theirsite.com/ http://www.dummy.com/ http://www.theirsite.com/stuff http://www.dummy.com/stuff http://www.theirsite.com/ stuff http://www.dummy.com/ stuff P:\test>perl 324406-2.pl < junk.txt $VAR1 = { 'www.theirsite.com' => 'our.server1/theirsite/', 'www.dummy.com' => 'our.server2/dummy/' }; http://our.server1/theirsite/ http://our.server2/dummy/ http://our.server1/theirsite/ stuff http://our.server2/dummy/ stuff http://our.server1/theirsite/ stuff http://our.server2/dummy/ stuff

You could probably use a better regex for getting the domain and one of the Config::* modules for parsing the ini file.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Timing (and a little luck) are everything!


In reply to Re: Organising lots of simple regexes by BrowserUk
in thread Organising lots of simple regexes by ViceRaid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.