Professional Web Applications Themes

text replacement+ - PERL Beginners

Hi everyone, I have a following data to yze : --------- 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) --------- I want to replace BlockA rcolor "1 1 1" to "4 4 4" and BlockB rcolor "1 1 1" to "0 0 0". I have variables like $BlockA_rcolor = "rcolor 4 4 4". ------------- while (<IN>) { if ( $_ =~ m/rcolor/ ) { $_ = $BlockA_rcolor; } print $_; } ------------- Above code will replace all 'rcolor' lines ...

  1. #1

    Default text replacement+


    Hi everyone,

    I have a following data to yze :
    ---------
    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)
    ---------

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

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

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

    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.

    Thanks in advance!
    -kiko

    Kiko Uehara Guest

  2. #2

    Default Re: text replacement+

    On Nov 14, 2003, at 3:57 PM, Kiko Uehara wrote:
    > Hi everyone,
    Howdy.
    > I have a following data to yze :
    > ---------
    > 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

    James Edward Gray II Guest

  3. #3

    Default Re: text replacement+

    Kiko Uehara wrote:
    >
    > Hi everyone,
    Hello,
    > I have a following data to yze :
    > ---------
    > 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)
    > ---------
    >
    > I want to replace
    > BlockA rcolor "1 1 1" to "4 4 4"
    > and
    > BlockB rcolor "1 1 1" to "0 0 0".
    >
    > I have variables like $BlockA_rcolor = "rcolor 4 4 4".
    >
    > -------------
    > while (<IN>)
    > {
    > if ( $_ =~ m/rcolor/ )
    > {
    > $_ = $BlockA_rcolor;
    > }
    > print $_;
    > }
    > -------------
    > Above code will replace all 'rcolor' lines in the data.
    >
    > 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.
    It looks like you should use paragraph mode and probably a hash:

    $/ = '';
    my %rcolor = ( A => '4 4 4', B => '0 0 0' );

    while ( <IN> ) {
    if ( /^\s+Block([A-Z])\b/ ) {
    my $key = $1;
    s/\b(rcolor\s+)\d+\s+\d+\s+\d+/$1$rcolor{$key}/;
    }
    print;
    }


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  4. #4

    Default Re: text replacement+

    On Nov 14, 2003, at 4:13 PM, James Edward Gray II wrote:
    > 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"};
    > }
    Oops, forgot the print() right here. Sorry.

    print;
    > }
    James

    James Edward Gray II Guest

  5. #5

    Default Re: text replacement+

    Kiko Uehara wrote:
    >
    > I have a following data to yze :
    > ---------
    > 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)
    > ---------
    >
    > I want to replace
    > BlockA rcolor "1 1 1" to "4 4 4"
    > and
    > BlockB rcolor "1 1 1" to "0 0 0".
    >
    > I have variables like $BlockA_rcolor = "rcolor 4 4 4".
    >
    > -------------
    > while (<IN>)
    > {
    > if ( $_ =~ m/rcolor/ )
    > {
    > $_ = $BlockA_rcolor;
    > }
    > print $_;
    > }
    > -------------
    > Above code will replace all 'rcolor' lines in the data.
    >
    > 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.
    Hi Kiko.

    Your description is a mixture of data and code. If you want us to
    look at your software then you need to post more than
    > I have variables like $BlockA_rcolor = "rcolor 4 4 4".
    But just from the identifiers in the data you have shown
    it looks like you need to alter a set of RGB values in
    a data file.

    If you have a clearer description of your source and your
    goal then we can help better. Are you changing a program
    that already exists, or are you writing from scratch? It's
    also usually better to show actual data if you can, rather
    than something that you think encapsulates the real thing.

    Rob


    Rob Dixon Guest

  6. #6

    Default Re: text replacement+

    On Nov 15, Kiko Uehara said:
    >---------
    > 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
    >---------
    > BlockA rcolor "1 1 1" to "4 4 4"
    > BlockB rcolor "1 1 1" to "0 0 0".
    >
    >I have variables like $BlockA_rcolor = "rcolor 4 4 4".
    I would suggest a hash instead:

    $change{BlockA}{rcolor} = "4 4 4";
    $change{BlockB}{rcolor} = "0 0 0";

    or:

    %change = (
    BlockA => {
    rcolor => "4 4 4",
    # other ones
    },
    BlockB => {
    rcolor => "0 0 0",
    # other ones
    },
    # other blocks
    );
    >while (<IN>)
    >{
    > if ( $_ =~ m/rcolor/ ) {
    > $_ = $BlockA_rcolor;
    > }
    > print $_;
    >}
    I'd suggest keeping track of what block you're in, and what "attribute" is
    on the line:

    BLOCK: while (<IN>) {
    # skip blank lines
    print, next if /^\s*$/;

    # otherwise extract the block name
    my ($block) = /^(\S+)/;

    # print the line
    print;

    # process the block's contents
    while (<IN>) {
    # restart the OUTER while loop if we're done with this block
    redo BLOCK unless /^\s+(\S+)/;

    # store the attribute
    my $attr = $1;

    # if this block has things to be modified,
    # and this attribute is one of them, change it
    if ($change{$block} and exists $change{$block}{$attr}) {
    s/\Q$attr\E.*/$attr $change{$block}{$attr}/;
    }

    # print the attribute
    print;
    }
    }

    Let me know if you understand it, or if you need more explanation.

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  7. #7

    Default Re: text replacement+

    Kiko Uehara wrote:
    > Hi everyone,
    >
    > I have a following data to yze :
    > ---------
    > 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)
    > ---------
    >
    > I want to replace
    > BlockA rcolor "1 1 1" to "4 4 4"
    $line =~ s/1 1 1/4 4 4/ if $last_block eq 'BlockA ';
    >
    > and
    > BlockB rcolor "1 1 1" to "0 0 0".
    $line =~ s/1 1 1/0 0 0/ if $last_block eq 'BlockB ';
    > If my question doesn't have enough information, please let me know.
    Sure doesn't. The most important information concerning any program is
    what real-world purpose [even if simulated] it serves. The coding comes
    later.
    > Thanks in advance!
    > -kiko
    From what you ahve given us, the lines above may help. You will just
    have to fill in the blanks


    R. Joseph Newton Guest

  8. #8

    Default Re: text replacement+

    On Nov 16, 2003, at 10:22 PM, R. Joseph Newton wrote:
    > Kiko Uehara wrote:
    >
    >> If my question doesn't have enough information, please let me know.
    >
    > Sure doesn't. The most important information concerning any program is
    > what real-world purpose [even if simulated] it serves. The coding
    > comes
    > later.
    I'm not sure I understand the new trend on this list to tell everyone
    they're not providing enough information. Two solutions were posted
    before these complaints. I know beginners often "simplify" what they
    think is the problem and make it hard if not impossible to help them,
    but I felt Kiko provided a lot of useful information. How many posters
    leave out the data file completely? I think we should be careful about
    which behaviors we are discouraging.

    James

    James Edward Gray II Guest

Similar Threads

  1. Adding replacement text for acronyms using the SDK
    By David_Hottman@adobeforums.com in forum Adobe Acrobat SDK
    Replies: 6
    Last Post: July 14th, 07:44 PM
  2. Contribute replacement
    By JSimmerin in forum Macromedia Contribute Connection Administrtion
    Replies: 3
    Last Post: April 15th, 01:28 AM
  3. Media replacement
    By B_Carlson@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 1
    Last Post: April 27th, 09:30 PM
  4. string replacement
    By Ovide in forum PHP Development
    Replies: 2
    Last Post: August 28th, 06:04 AM
  5. text replacement in a memo filed
    By Allen Browne in forum Microsoft Access
    Replies: 0
    Last Post: June 30th, 04:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139