Newsgroups: comp.lang.perl Path: utzoo!utgpu!watserv1!watdragon!rose!ccplumb From: ccplumb@rose.uwaterloo.ca (Colin Plumb) Subject: Re: Help with split, replace and number formats. Message-ID: <1991Feb15.225320.1069@watdragon.waterloo.edu> Sender: daemon@watdragon.waterloo.edu (Owner of Many System Processes) Organization: University of Waterloo References: <1991Jan29.030541.46@eagle.inesc.pt> Date: Fri, 15 Feb 1991 22:53:20 GMT Lines: 48 jmc@eagle.inesc.pt (Miguel Casteleiro) wrote: >Hi there! > >I have some problems that I need to solve so I can finish some >perl scripts. > >1) I need to split the following line: > > "This is a line ( test,with ugly typing." > >into the array: > > ('"This','is','a','line','(','test,','with','ugly','typing."') > >Please note the punctuation characters. >Is there a split pattern to do this? Well, it depends on details, but defining a word as \w+, whitespace as \s+, and punctuation as [^\w\s]+, and assuming you want to split after punctuation and before words, even if there is no explicit space, then just add one and split as usual: s/([^\w\s]+)(\w)/\1 \2/g split; >2) I need to replace the word: 'teste' > by the word: 'aebtecd' > >In this replace operation the character 't' gets replaced by 'a' >and 'c' is appended to the word, and the character 's' is replaced >by 'b' and 'd' is appended to the word. >I need this strange replace to use a 7-bit sort to sort 8-bit >(ISO-8859-1) text. >What I need is to translate some characters by some others, and >for each character translated I need to append a character to the >string (something like: tr/ts/ab/cd/ :-). >Is there an easy way to do this? Yes. $suffix = $_; $suffix =~ tr/ts//cd; # delete anything other then t and d # ^^ This 'cd' has *nothing* to do with the one below! $suffix =~ tr/ts/cd/; # map t and s to c and d tr/ts/ab/; $_ .= $suffix; -- -Colin