On Nov 14, 2003, at 3:57 PM, Kiko Uehara wrote:

Hi everyone,

Howdy.


I have a following data to analyze :
---------
 BlockA
        color 0 0 0
        rcolor 1 1 1
        dcolor 2 2 2

 BloackB
        color 0 0 0
        rcolor 1 1 1
        dcolor 2 2 2
(...and so on)
---------

With ya so far.


I want to replace
 BlockA rcolor "1 1 1" to "4 4 4"
 and
 BlockB rcolor "1 1 1" to "0 0 0".

Well, that should be pretty easy. Let's see what we can think up...


I have variables like $BlockA_rcolor = "rcolor 4 4 4".

Why don't we change that to a hash. Something like:


my %changes = ( BlockA_rcolor => '4 4 4', BlockB_rcolor => '0 0 0' );

-------------
while (<IN>)
{
    if ( $_ =~ m/rcolor/ )
    {
        $_ = $BlockA_rcolor;
    }
    print $_;
}
-------------
 Above code will replace all 'rcolor' lines in the data.

It sure will, now let's see if we can use the hash to make it more flexible:


my $block;
while (<IN>) {
        if (/^(Block[A-Z]+)/) { $block = $1; }
        elsif (/^\s+([rd]?color)/) {
                my $color = $1;
                s/\d+ \d+ \d+$/$changes{"${block}_$color"}/
                                if exists $changes{"${block}_$color"};
        }
}

Can you follow how that works?

It makes it easy to update ANY color in ANY block. Just add the right hash entry.

Can anyone please give me an idea, what kind of method I could use ?
If my question doesn't have enough information, please let me know.

Your question was perfect. Good luck.


James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to