[26353] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8526 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 15 18:05:30 2005

Date: Sat, 15 Oct 2005 15:05:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 15 Oct 2005     Volume: 10 Number: 8526

Today's topics:
    Re: match 1/2/3, replace with a/b/c <RedGrittyBrick@SpamWeary.foo>
    Re: match 1/2/3, replace with a/b/c <RedGrittyBrick@SpamWeary.foo>
    Re: match 1/2/3, replace with a/b/c <joe@rockhead.spamfree.freespam.com>
    Re: match 1/2/3, replace with a/b/c <joe@rockhead.spamfree.freespam.com>
    Re: match 1/2/3, replace with a/b/c <abigail@abigail.nl>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 15 Oct 2005 15:31:05 +0000 (UTC)
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: match 1/2/3, replace with a/b/c
Message-Id: <dir7bp$cek$1@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com>

joe rockhead wrote:
> 
> I'm not sure what the name of the concept is, but here goes.

Forgive me, but I think your concept is a muddled one.


> I want to find  1 or 2 or 3 and replace it with a or b or c.

No you don't, that would mean "1" could be replaced by "c", say.

> if it finds 1, replace it with a.
> 2 => b
> and 3 => c.

The above is clearer, you want three separate and distinct substitutions.

s/1/a/; s/2/b/; s/3/c/;

> 
> I understand that
> s/1|2|3/x/
> replaces 1,2 or 3 with x.
> 
> anyway here's what I'm doing about it:
> 
> 
> use IO::All;
> my @list = io('list.txt')->slurp;
> my $message < io 'passage.txt';

I doubt that compiles!

To read files I'd use
   open my $message, '<', 'passage.txt'
     or die "Unable to open passage.txt: $!";
   while (<$message>) {
     # do stuff
   }
   close $message or die;

> 
>     for (@list){
>         my ($preFilter, $postFilter) = split /=/, $_;
>         $message =~ s/$preFilter/$postFilter/g;
>     }

Did you actually try it? What errors did you see?


> 
> 
> Where list.txt:
> 486DX=P4
> 640KB=2GB
> 50MB HDD=400GB HDD
> 1.44MB FDD=Flash Media Reader
> 6X CD-ROM=6X DL DVD
> 
> and message.txt:
> My blasing fast 486DX with 640KB of RAM
> 50MB HDD 1.44MB FDD
> 6X CD-ROM
> 
>
#!perl
use strict;
use warnings;

my %patterns = (
   '486DX'      => 'P4',
   '640KB'      => '2GB',
   '50MB HDD'   => '400GB HDD',
   '1.44MB FDD' => 'Flash Media Reader',
   '6X CD-ROM'  => '6X DL DVD'
);

while (<DATA>) {
   foreach my $key (keys %patterns) {
     s/$key/$patterns{$key}/eg;
   }
   print;
}
__DATA__
My blasing fast 486DX with 640KB of RAM
50MB HDD 1.44MB FDD
6X CD-ROM


Output:
My blasing fast P4 with 2GB of RAM
400GB HDD Flash Media Reader
6X DL DVD


------------------------------

Date: Sat, 15 Oct 2005 17:14:41 +0000 (UTC)
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: match 1/2/3, replace with a/b/c
Message-Id: <dirde0$1fq$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>

RedGrittyBrick wrote:
> #!perl
> use strict;
> use warnings;
> 
> my %patterns = (
>   '486DX'      => 'P4',
>   '640KB'      => '2GB',
>   '50MB HDD'   => '400GB HDD',
>   '1.44MB FDD' => 'Flash Media Reader',
>   '6X CD-ROM'  => '6X DL DVD'
> );
> 
> while (<DATA>) {
>   foreach my $key (keys %patterns) {
>     s/$key/$patterns{$key}/eg;
>   }
>   print;
> }

You know, its often just after hitting the send button that I think of a 
much better way of naming variables or some other such thing:

#!perl
use strict;
use warnings;

my %upgrade = (
   '486DX'      => 'P4',
   '640KB'      => '2GB',
   '50MB HDD'   => '400GB HDD',
   '1.44MB FDD' => 'Flash Media Reader',
   '6X CD-ROM'  => '6X DL DVD'
);

while (<DATA>) {
   foreach my $feature (keys %upgrade) {
     s/$feature/$upgrade{$feature}/eg;
   }
   print;
}


Ho hum.


------------------------------

Date: Sat, 15 Oct 2005 14:04:48 -0400
From: joe rockhead <joe@rockhead.spamfree.freespam.com>
Subject: Re: match 1/2/3, replace with a/b/c
Message-Id: <gP6dnXGCB-9f2czeRVn-hw@speakeasy.net>

I don't know who to answer back to.
so, how about this.
I'm probably delusional, but I thought perhaps I may have seen something like this.

in a s///, you can use the | to find item1 or item2 or item3 and replace it with something.

so, is that a reges where I can s/// look for item1 replace it with something1
item2 with something2
and item 3 with something3 at the same time?
in one line of regex?

The code I put up before worked and did the job, but I just thought I may have seen a substitution that did it in
one line.

the hash that one of you put up works also.
and yes, I do own the orielly regex book and I did read it.
i just don't know if what i'm looking for exists and if it does what's it called?


------------------------------

Date: Sat, 15 Oct 2005 15:03:18 -0400
From: joe rockhead <joe@rockhead.spamfree.freespam.com>
Subject: Re: match 1/2/3, replace with a/b/c
Message-Id: <XeGdnULB2eXlz8zeRVn-vg@speakeasy.net>

RedGrittyBrick wrote:
> joe rockhead wrote:
 ...
> 
> The above is clearer, you want three separate and distinct substitutions.
> 


> s/1/a/; s/2/b/; s/3/c/;

ah, yes.
perhaps this one.
can this be done in one step?
and what would it be called?


------------------------------

Date: 15 Oct 2005 19:20:37 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: match 1/2/3, replace with a/b/c
Message-Id: <slrndl2lk5.f1.abigail@alexandra.abigail.nl>

joe rockhead (joe@rockhead.spamfree.freespam.com) wrote on MMMMCDXXVIII
September MCMXCIII in <URL:news:XeGdnULB2eXlz8zeRVn-vg@speakeasy.net>:
##  RedGrittyBrick wrote:
## > joe rockhead wrote:
##  ...
## > 
## > The above is clearer, you want three separate and distinct substitutions.
## > 
##  
##  
## > s/1/a/; s/2/b/; s/3/c/;
##  
##  ah, yes.
##  perhaps this one.
##  can this be done in one step?
##  and what would it be called?


    my %r = qw {1 a 2 b 3 c};
    my $p = join "|" => keys %r;
    s/($p)/$r{$1}/g;


Abigail
-- 
perl -wle'print"Κυστ αξοτθες Πεςμ Θαγλες"^"\x80"x24'


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8526
***************************************


home help back first fref pref prev next nref lref last post