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"; }