Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Comapring two values of a scalar

by igelkott (Curate)
on Apr 13, 2013 at 12:24 UTC ( #1028513=note: print w/replies, xml ) Need Help??

in reply to Comapring two values of a scalar

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

Replies are listed 'Best First'.
Re^2: Comapring two values of a scalar
by BillKSmith (Priest) on Apr 13, 2013 at 17:47 UTC

    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;

      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1028513]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2016-10-22 11:55 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (294 votes). Check out past polls.