Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Regular Expression I think.

by ergowolf (Monk)
on Sep 26, 2000 at 22:38 UTC ( [id://34077]=perlquestion: print w/replies, xml ) Need Help??

ergowolf has asked for the wisdom of the Perl Monks concerning the following question:

I would like change a list. I want to preserve the values, but change everything up the the =.

I have list A
This=That
Some=Song

To list B
Other=That
Corny=Song

How would I go about this?

Ergowolf Does code make a sound if no one is there to type it?

Replies are listed 'Best First'.
Re: Regular Expression I think.
by Fastolfe (Vicar) on Sep 26, 2000 at 22:45 UTC
    Update: A regular expression would allow you to modify the text "in place" in your arrays. Others have suggested using split to break the text apart, which would work fine (and more efficiently) if you're wanting to just get at the text on either side of the equals sign. You could make your changes and re-combine the new left side and the old right side when you were done if you wanted. Otherwise...

    I'm assuming that you mean that you have a list built sorta like this:

    @listA = ('This=That', 'Some=Song'); @listB = ('Other=That', 'Corny=Song');
    If you wish to change the text, you'd start off with this regular expression:
    $text =~ s/.+?=/$new_text=/;
    This will replace everything up to the equal sign with the text in $new_text. Now, to adapt it to modify the items in your arrays:
    foreach (@listA, @listB) { # or just one of the two, whatever # set $new_text to whatever you want s/.+?=/$new_text=/; }
    The trailing = sign after $new_text is necessary because my pattern (trying to keep it simple) also included an equal sign. You could just as easily do this:
    s/.+?(?==)/$new_text/;
    Hope this answers your question.
Re: Regular Expression I think.
by chromatic (Archbishop) on Sep 26, 2000 at 23:06 UTC
    How about something like this? It requires that you know what kind of changes you'll make:
    #!/usr/bin/perl -w use strict; # set up our changes my @lines = qw( This=That Some=Song ); my %changes = ( This => 'Other', Some => 'Corny' ); foreach my $line (@lines) { # for all the money $line =~ s/([^=]+)=/$changes{$1} . "="/e; } print join("\n", @lines), "\n";

    Update: dchetlin is right, there's no need for /e. Oops.

      This is a good answer, and there are no problems with it.

      Just wanted to point out that it's one of those situations where the /e modifier seems like it's needed, but it's not.

      s/([^=]+)=/$changes{$1} . "="/e is effectively the same as s/([^=]+)=/$changes{$1}=/, but the latter is more efficient..

      On the other hand, using [^=]+ is a much cleaner and more efficient (and less error prone) solution than .*? which appears once or twice elsewhere in this thread.

      -dlc

Re: Regular Expression I think.
by fundflow (Chaplain) on Sep 26, 2000 at 22:51 UTC
    This should work for you:
    %dict=('This' => 'Other', 'Some' => 'Corny'); while (<>) { ($left, $right) = split(/=/); die unless defined($dict{$left}); print $dict{$left} . "=" . $right . "\n" }
Re: Regular Expression I think.
by little (Curate) on Sep 26, 2000 at 22:42 UTC
    mmh,
    what about splitting the string at the "=", so have two parts you can do with what you want?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://34077]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found