Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

loop to remove negatives

by BluGeni (Novice)
on May 14, 2012 at 19:08 UTC ( #970488=perlquestion: print w/ replies, xml ) Need Help??
BluGeni has asked for the wisdom of the Perl Monks concerning the following question:

hello i am trying to remove negatives that may be entered into a text box by users with a loop instead of using something repetitive like
$busi1 =~ s/-//g; $busi2 =~ s/-//g; $busi3 =~ s/-//g;
ect.. ect.. i would rather loop it. i have tried
for($i=0 $i<31;$i++) { $busi[$i] =~ s/-//g; }
but it doesn't seem to do the job. any help would be great, thank you.

Comment on loop to remove negatives
Select or Download Code
Re: loop to remove negatives
by Eliya (Vicar) on May 14, 2012 at 19:14 UTC

    Use the aliasing feature of for

    for ($busi1, $busi2, $busi3) { s/-//g; }

    I.e., the indidual items you loop over are being aliased to $_ in this case.

    Or use an array instead of individual variables named $var1, $var2, ..., in which case the above would reduce to

    for (@busi) { s/-//g; }

    (But naturally, when you want to refer to a single value only in other places of your code, you'd then have to say, for example, $busi[2])

Re: loop to remove negatives
by Kenosis (Priest) on May 14, 2012 at 22:50 UTC

    Hi, BluGeni.

    I understand wanting to remove negative signs within a block of text. However, you show variables $busi1 .. $busi3 in substitution operations, and a variable $i taking values 0 .. 31 as a subscript used in a for loop, viz., $busi[$i]. Additionally, you mention "negatives that may be entered into a text box," perhaps suggesting a single text box within a Web form.

    How many text boxes are you working with in your Web form: 1, 3, 32, other? How is/are the value(s) from the Web form accessed from within your script (e.g., by using CGI's params function)?

Re: loop to remove negatives
by Athanasius (Monsignor) on May 15, 2012 at 04:31 UTC

    Hi BluGeni,

    You should prefer the more idiomatic syntax in Eliya’s solution.

    Just thought I’d mention that the code snippet you give, which you say “doesn’t seem to do the job,” should actually work fine, except that there’s a semicolon missing after $i=0; in the for loop:

    #!/usr/bin/perl use strict; use warnings; my @busi = ( -1, -42, 7, -17 ); for (my $i = 0; $i < @busi; $i++) { $busi[$i] =~ s/-//g; print "\$busi[$i] = $busi[$i]\n"; } __END__

    Gives me:

    $busi[0] = 1 $busi[1] = 42 $busi[2] = 7 $busi[3] = 17

    as required.

    HTH,

    Athanasius <°(((><contra mundum

      The code given in the OP would work fine, provided that all the values are in an array (@busi). If they're independent scalars ($busi1, $busi2, etc., as in the example of what currently works), then it won't.

      BluGeni, I strongly recommend putting your values into an array, then using

      for (@busi) { $_ =~ s/-//g; }
      (or, more idiomatically, s/-//g for @busi;) instead of working with the variables as independent scalars - any time you have a series of variables named $foo1, $foo2,..., that's a pretty strong indication that what you really want is an array.

      If you need help getting these values into an array, just show us the code that you're currently using to assign their values and we can show you how to modify it to use an array instead.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-08-22 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (148 votes), past polls