Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey fellows, I have this script:
#!/usr/bin/perl use CGI::Carp qw/fatalsToBrowser/; $number1 = '$64,200'; $number2 = '$64,200.00'; $number1 =~ s/\..*//; $number2 =~ s/\..*//; $number1 =~ tr/$,//d; $number2 =~ tr/$,//d; if ($number1 > $number2) { print "Content-type: text/html\n\n"; print "$number1 is greater than $number2\n"; } elsif ($number1 < $number2) { print "Content-type: text/html\n\n"; print "$number1 is less than $number2\n"; } elsif ($number1 == $number2) { print "Content-type: text/html\n\n"; print "$number1 is equal to $number2\n"; }
But when it prints I want $number1 and $number2 to print as the original ($number1 = '$64,200';) not as modified ($number1 =~ s/\..*//;). How can i do this?

Replies are listed 'Best First'.
Re: How do i print a variable back when it has been altered?
by grinder (Bishop) on Dec 22, 2001 at 03:53 UTC
    You want to save a copy of the variable before you alter it, and then print it out afterwards.
    my $number1_original = $number1; $number1 =~ s/\..*//; $number1 =~ tr/$,//d; ... print "Content-type: text/html\n\n"; print "$number1_original is equal to $number2_original\n";

    You should also use strict in your code, as well as place -w in the first line, to warn for simple errors. I think that would be of great benefit to you.

    Just one question, after that last elsif, what would you put in an else following on from it?...

    Of course, you could always do away with the if/elsif code by using:

    print "$number1_original is ", ('equal to', 'greater than', 'less than')[ $number1 <=> $number2 ], " $number2_original\n";

    But that's maybe for another day.</p. --

    g r i n d e r
    just another bofh

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
      ('equal to', 'greater than', 'less than')[ $number1 <=> $number2 ],

      Neat trick grinder! Much better than nested ternary expressions.

      dmm

      You can give a man a fish and feed him for a day ...
      Or, you can
      teach him to fish and feed him for a lifetime
Re: How do i print a variable back when it has been altered?
by dmmiller2k (Chaplain) on Dec 22, 2001 at 04:18 UTC

    The short answer is save copies of the variables and print those. An easy way to do that is to use local within a block scope:

    # ... $res = 0; { local ($number1, $number2)=($number1, $number2); # localize the val +ues $number1 =~ s/\..*//; $number2 =~ s/\..*//; $number1 =~ tr/$,//d; $number2 =~ tr/$,//d; # returns -1, 0 or 1, depending upon comparison $result = $number1 <=> $number2; } # $number1 and $number2 revert to original values when scope is exited # why duplicate this thrice? print "Content-type: text/html\n\n"; print "$number1 is ". ( ($result==0) ? 'equal to' : ($result <0) ? 'less than' : 'greater than' ) ." $number2\n" +;

    Update: (see grinder's reply, above)

    print "$number1 is ". ('equal to', 'greater than', 'less than')[$result] ." $number2\n";

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime