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

remove lending figures

by frank1 (Monk)
on Sep 18, 2025 at 17:10 UTC ( [id://11166266]=perlquestion: print w/replies, xml ) Need Help??

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

i have a JSON response of this

0.0  #time dot seconds

my question is how to get lending figures before (.) dot and have output 0

same applies to below

0:00 #time and seconds

my question is how to get lending figures before (:) dots and have output 0

Replies are listed 'Best First'.
Re: remove lending figures
by choroba (Cardinal) on Sep 18, 2025 at 18:49 UTC
    Hard to tell. What does "lending" mean in this context? The ones before or after the dot(s)?

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Cpanel::JSON::XS; my $json = '{"dot":"0.0","colon":"0:00"}'; my $structure = Cpanel::JSON::XS->new->decode($json); for my $type (keys %$structure) { say "$type: ", $structure->{$type} =~ s/[.:].*//r; }

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: remove lending figures
by starX (Chaplain) on Sep 27, 2025 at 14:59 UTC
    If by "lending" you mean "leading," the split function will do this for you. i.e.
    my $json = "0.0"; my ($minutes, $seconds) = split(/[.:]/, $json);
    By placing the . and the : into a character class (in the []), split will work on either one.
Re: remove lending figures
by Anonymous Monk on Sep 18, 2025 at 18:41 UTC
    just do int($whatever) since you probably don't use warnings anyway

    disclaimer: lazy question, lazy answer

      Thanks, i think this will help me int($whatever)

      because i did some little demonstration and works good

      $string = "1.00"; if (int ($string > 1)) { print "Yes true"; } else { print "Not true"; }

        tho with 1:00, isn't working

        am getting this error Argument "1:00" isn't numeric in numeric gt (>) at HelloWorld.pl line 4.

        $string = "1:00"; if (int ($string > 3)) { print "Yes true"; } else { print "Not true"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2026-03-11 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.