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

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) {
with
foreach my $line ( @empty_square) { my $boxLine = $line;
I hope this makes it clearer. --Sparkey


Comment on Re: Question of variable interpolation in regex
Select or Download 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 | +----+
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 | | | | | | | | | | | +----+----+----+----+
      Update:
      Take a look at my scratch pad for the full code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://213201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-11-20 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (102 votes), past polls