Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

write off function using regex

by suhailck (Friar)
on May 24, 2010 at 06:43 UTC ( #841328=perlquestion: print w/ replies, xml ) Need Help??
suhailck has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm trying to write a writeoff function using regex

It should work as follows,
writeoff(9.91) => 9.90
writeoff(9.92) => 9.90
.
writeoff(9.94) => 9.90
writeoff(9.95) => 9.95
.
.
writeoff(9.99) => 9.95

Below is the code i tried
perl -le '($_=shift)=~s/(\.[0-9])([0-9])/$1(?($2>=5)5|0)/;print' 9.97

OUTPUT
9.9(?(7>=5)5|0)
Please let me know where im doing wrong and other regex implementation of the same
Thanks in advance!!

Comment on write off function using regex
Download Code
Re: write off function using regex
by ikegami (Pope) on May 24, 2010 at 07:04 UTC
    • You're trying to use a regex pattern where a string literal is expected.
    • You're think a regex pattern generates the strings it can match in some circumstances.

    Some solutions:

    s/\.[0-9]\K(?:[0-4]|([5-9]))/$1?5:0/e
    s{\.[0-9]\K([0-9])}{int($1/5)*5}e;
    s{(.*)}{sprintf("%.2f", int($1*20)/20)}e;
    $_ = sprintf("%.2f", int($_*20)/20) . /(?!)/; # oblig regex
Re: write off function using regex
by Anonymous Monk on May 24, 2010 at 07:24 UTC
    I'm not sure what you're trying , but I think this is it
    #!/usr/bin/perl -- use strict; use warnings; use Test::More qw( no_plan ); Main(@ARGV); exit(0); sub writeoff { my ( $prefix, $last ) = shift()=~ /^(.+)(.)$/; $last = $last < 5 ? 0 : 5; return "$prefix$last"; } sub Main { my @input = qw( 9.91 9.92 9.95 9.99 ); my @wanted_output = qw( 9.90 9.90 9.95 9.95 ); my @got = map { writeoff($_) } @input; is_deeply( \@got, \@wanted_output, "@wanted_output" ); } __END__ ok 1 - 9.90 9.90 9.95 9.95 1..1
Re: write off function using regex
by Krambambuli (Deacon) on May 24, 2010 at 08:24 UTC
    Maybe something like
    #!/usr/bin/perl use strict; use warnings; use POSIX; my @data = qw( 9.90 9.91 9.9111 9.94999 9.95 9.950001 9.96 9.99 9.9999 +9); foreach my $x (@data) { printf "%s => %.2f\n", $x, floor( 20*$x) /20, "\n"; }
    which outputs
    
    9.90 => 9.90
    9.91 => 9.90
    9.9111 => 9.90
    9.94999 => 9.90
    9.95 => 9.95
    9.950001 => 9.95
    9.96 => 9.95
    9.99 => 9.95
    9.99999 => 9.95
    
    
    would fit too ?

    Your wanted writeoff function is then simply floor( 20 * x) /20.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-07-14 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls