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

I'm having a bit of a problem understanding the next script:
$time=$16; # Time Sample @_1= split "\,",$16; if($#_1 eq 0){ $time = int ($time); } if($#_1 eq 1){ $time=$_1[0]; $time=~ s/\,//g; $time*=1000; } if($#_1 eq 2){ $time=1000;} if($#_1 eq 3){$time=1000;}
Thanks

update (broquaint): added <code> tags

Replies are listed 'Best First'.
Re: split and compare
by castaway (Parson) on May 20, 2003 at 11:04 UTC
    $time=$16; # Time Sample - 1000,000.00 @_1 = split "\,", $16; # Split the value in $16 (should be $time!) + where commas occur, put the results in @_1. (eg 1000 and 000.00) if($#_1 eq 0){ # if there is one value in @_1 (ie no comma +s in $time) $time = int ($time); # set $time to the integer value of $time } if($#_1 eq 1){ # if there are 2 values in @_1 (at least on +e comma) $time=$_1[0]; # set $time to the first value (1000 in my +example) $time=~ s/\,//g; # replace all the commas in $time with noth +ing $time*=1000; # multiply $time by 1000 } if($#_1 eq 2){ $time=1000;} # If there are 3 values in @_1 , set $t +ime to 1000 if($#_1 eq 3){$time=1000;} # If there are 4 values in @_1 , set $t +ime to 1000
    It's terrible code, and I don't even think its working as it should (why set $time to $_1[0] and then replace commas which dont exist in there?)

    I think overall it's trying to parse a number where the thousands are separated by commas, into an integer. I'm sure there are better ways/modules to do that.

    C.

      > I think overall it's trying to parse a number where the
      > thousands are separated by commas, into an integer.

      To do this it would probably be enough to remove all the commas and then either, depending on the result you want, round() o sprintf("%d") the resulting string.

      The (awful) code may be doing something more, however an example of the input would be nice to study. ;)

      Michele.
Re: split and compare
by Skeeve (Parson) on May 20, 2003 at 10:56 UTC
    Okay! My Attempt:
    $time=$16; # Time Sample
    There is a gigantic regexp before that with at least 16 brackets '(...)'. $time gets what's found in the 16th.
    @_1= split "\,",$16;
    That value is split at the kommas (is it? Shouldn't it be /,/?)
    if($#_1 eq 0){ $time = int ($time); }
    If there was no komma, $time becomes the integer value of itself.
    if($#_1 eq 1){ $time=$_1[0]; $time=~ s/\,//g; $time*=1000; }
    If there was a komma, $time becomes 1000 times the value of the value just left of the first komma. $time=~ s/\,//g; should be unneccessary as there can't be any komma in $_1[0].
    if($#_1 eq 2){ $time=1000;} if($#_1 eq 3){$time=1000;}
    If there was any other numper of kommas, $time becomes 1000.

    Don't ask me what this is good for!

Re: split and compare
by bart (Canon) on May 20, 2003 at 10:52 UTC
    $16 (a read only value to begin with) and @_1 are horrible names for variables. Please don't do that.

    And what does your data (the "Time Sample") look like? It's hard for me to follow if it's not clear what it's supposed to be working on. As I am sure it is the same for you, too.

Re: split and compare
by pzbagel (Chaplain) on May 20, 2003 at 16:26 UTC
    This code is painful, bu let's go line by line:
    # Splits the contents of $16 on commas into array @_1 @_1= split "\,",$16; # If @_1 has only one element force $time to integer # Does $time already have a value? That's not apparent in # this snippet. if($#_1 eq 0){ $time = int ($time); } # If @_1 has two elements set $time to the first one # remove any commas (Although those should have been taken # care of by the split above) # And multiply $time by 1000 if($#_1 eq 1){ $time=$_1[0]; $time=~ s/\,//g; $time*=1000; } # If @_1 has 2 or 3 elements simply set $time to 1000 if($#_1 eq 2){ $time=1000;} if($#_1 eq 3){$time=1000;}

    This code seems like it is splitting up a large number with commas in it a la 1,000,000,000 or something. Perhaps Number::Format could be used?

    HTH