Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comapring two values of a scalar

by bonny (Initiate)
on Apr 13, 2013 at 11:41 UTC ( [id://1028506]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I have one question, suppose a variable is $array[0]= \31:15\; Now I want to find the difference between two numbers... here, (31-15) please guide me. PS: this variable will be assigned different values at different time, i.e \15:0\, \7:0\, \127:0\, \127:15\ etc...

Replies are listed 'Best First'.
Re: Comapring two values of a scalar
by igelkott (Priest) on Apr 13, 2013 at 12:24 UTC

    Looks like you tried to write \[31:15\] rather than use code tags. See Markup in the Monastery for help with posting questions.

    Taking a bit of a guess but you probably have something like $array[0]= "31:15";. If so, just split on the ":" and do the subtraction. This should be simply enough for a beginner (sorry for the assumption) but if it gets confusing, try the same thing on a simple variable (scalar) like $foo= "31:15";.

      Uner the same assumption, I prefer a regular expression. The Common module will match any valid number. The /e option will do the subtraction. The /r option returns the result without modifying the original string.

      use strict; use warnings; use Regexp::Common; my $NUMBER = qr/$RE{num}{real}/; my @array = ('31:15'); print $array[0] =~ s/($NUMBER):($NUMBER)/$1-$2/er;
      Bill
        Regexp::Common

        While nice and general, this seems a bit heavy for the OP, especially considering the the rather regular examples given. Of course, always nice to see an example of a more general/powerful solution.

Re: Comapring two values of a scalar
by LanX (Saint) on Apr 13, 2013 at 11:49 UTC
    Plz format your posting according to Markup in the Monastery

    And then plz try to rephrase your question, the code given doesn't look like Perl and your examples are cryptic...

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2025-12-16 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (96 votes). Check out past polls.

    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.