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 PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<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.  ]


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

Reply via email to