Path: utzoo!attcan!uunet!zaphod.mps.ohio-state.edu!usc!ucsd!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Pattern Substitution Question Keywords: pattern Message-ID: <1990Oct29.200349.15909@iwarp.intel.com> Date: 29 Oct 90 20:03:49 GMT References: <1086@dg.dg.com> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 40 In-Reply-To: blurie@dg-rtp.dg.com (Benjamin Lurie) In article <1086@dg.dg.com>, blurie@dg-rtp (Benjamin Lurie) writes: | Here's a question, whose answer I'm sure is going to be trivial, that I've | been trying to resolve for days. Given input like the following: | | Lurie;Benjamin;1212 Nowhere Road;(617) 923-0000;(w) (000) 202-2343 | | I would like to translate it to: | | Benjamin Lurie;1212 Nowhere Road;(617) 923-0000;(w) (000) 202-2343 | | I've tried a pattern match looking for a semi-colon, but I always get the | longest pattern that matches, which would include everything up to the "(w)". assuming $_ = "Lurie...2343\n"... it'd be: s/^([^;]*);([^;]*);/$2 $1;/; Or, if you prefer thinking in fields, something like: @f = split(/;/); $_ = join(";", "$f[1] $f[0]", @f[2..$#f]); and to be totally clean about it, chop if /\n$/; @f = split(/;/); $_ = join(";", "$f[1] $f[0]", @f[2..$#f]) . "\n"; (which works even if you move around the last field). The last two are incorrect inside a general-purpose subroutine, which ought to take $[ into consideration, but I'll presume that's not a problem for you yet. $_ = "another;Just;hacker;Perl"; @f = split(/;/); print "@f[1,0,3,2],"; -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel put the 'backward' in 'backward compatible'..."=========/