Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Question of variable interpolation in regex

by SparkeyG (Curate)
on Nov 15, 2002 at 16:56 UTC ( #213201=note: print w/replies, xml ) Need Help??

in reply to Question of variable interpolation in regex

A few people commented that I wasn't clear in my question and also asked for example input and output. The input in this loop is:
my @empty_square = qw(+----+ |....| |....| |....| +----+ );
We iterate over several of these boxes, and need to replace . with spaces and a number in the upper left hand corner. I have some logic, that isn't included hear, to determine when to add the number.

The desired output of this loop would be if $putNum is true, the replace |....| with |1   | the first time, |2   | the second, etc. However, all it does now, if $putNum is true, it replaces all |....| with |1   |.

I can fix this if I replace the following:

foreach my $boxLine ( @empty_square) {
foreach my $line ( @empty_square) { my $boxLine = $line;
I hope this makes it clearer. --Sparkey

Replies are listed 'Best First'.
Re: Re: Question of variable interpolation in regex
by l2kashe (Deacon) on Nov 15, 2002 at 19:27 UTC
    Ok I saw a couple of things..

    1) In your code you incremented your value even though the first line doesnt match "/\./". So you always start at 2 instead of 1.
    2) you were using sprintf for your output when perl already had what you wanted to do built in via the nifty 'x' operator...

    I also changed the var names, just cause I could :)
    So here is what I came up with
    #!/usr/local/bin/perl -wT use strict; my($putNum,$line,$s_count,$num,$n_line, @square); $putNum = 1; @square = ( '+----+', '|....|', '|....|', '|....|', '+----+' ); foreach $line (@square) { if (!$putNum) { $line =~ s/\./ /g; } else { if ($line =~ /\./) { $s_count = $line =~ s/\./\./g; $num++ if ($s_count); $n_line = "$num" . ' ' x ($s_count - length($num)); $line =~ s/\.+/$n_line/o; } } print "$line\n"; } # OUTPUT +----+ |1 | |2 | |3 | +----+
    Is this what you were looking for? The same number of spaces that were previously '.'s on the line?

    Happy Hackin :)

    /* And the Creator, against his better judgement, wrote man.c */
      I solved the 1st problem you rose when $putNum is decided. If the line being considered is either the top or bottom line, $putNum is false.

      As to your second point, tmtowtdi. ;-)

      But I see I still haven't been clear. :-(
      the output I'm seeing is

      +----+----+----+----+ |1 |1 |1 |1 | | | | | | | | | | | +----+----+----+----+
      When what I want is
      +----+----+----+----+ |1 |2 |3 |4 | | | | | | | | | | | +----+----+----+----+
      Take a look at my scratch pad for the full code
Re: Re: Question of variable interpolation in regex
by Thelonius (Priest) on Nov 15, 2002 at 17:17 UTC
    Dunno, it works for me on perl 5.6.0, 5.6.1, and 5.8.0. Which version are you using?
    print join("\n", @empty_square), "\n"; +----+ |2 | |3 | |4 | +----+

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://213201]
[choroba]: needed everywhere, available rarely
[LanX]: Question: starting a script from an icon in Windows, but after C-c the window closes ... what I want is to restart in the cmd.exe. Recommendations?
[Corion]: LanX: What do you mean by "restart in the cmd.exe" ?
[Corion]: Do you want to launch a script and keep the command prompt/console window open?
[Corion]: Do you want to wait for a key press before closing the window?

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2017-03-27 15:42 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.