![]() ~~~~~~ The demonstration follows ~~~~~ use File::Package The results from executing the Perl Code follow on the next lines as comments. The str2int returns the remaining string data in and the array of integers DEMONSTRATION # The str2int subroutine, in an array context, supports converting multiple run of integers in an array of strings to an array of integers, It keeps converting the strings, starting with the first string in continuing to the next and next until it fails a conversion. The Data::SecsPack program module superceds the Data::StrInt program module. The scalar str2int is the same and supercedes C&. This makes it not only useful for forcing an integer conversion but also for testing a scalar to see if it is in fact an integer scalar. A non-numeric string, decimal or floating string returns an "undef" instead of the 0 and a warning that 0+'hello' produces. The scalar str2int subroutine performs the conversion to an integer for strings that look like integers and actual integers without generating warnings. $x = str2int(1E30) # $x is undef no warning $x = str2int(0.5) # $x is undef no warning $x = str2int('hello') # $x is undef no warning Instead use str2int uses a few simple Perl lines, without any evals starting up whatevers or firing up the regular expression engine with its interpretative overhead, to provide a slightly different response as follows:>. What is $x for the following: my $x = 0 + '0x100' # $x is 0 with a warning Surprising not all Perls, some Microsoft Perls in particular, may leave the internal storage as a scalar string. If it cannot perform the conversion, it leaves the integer 0. ![]() Perl itself has a documented function, '0+$x', that converts a scalar to so that its internal storage is an integer (See p.351, 3rd Edition of Programming Perl). $integer = str2int($string, = str2int($string, = a scalar context, the Data::SecsPack program module translates an scalar string to a scalar integer. The str2int subroutine is the same as the str2integer subroutine except that that the subroutine always returns the scalar processing str2integer subroutine. str2int $integer = $secspack->str2int($string) This is especially useful for $string that is certain to have a single number. In a scalar context, it parse out any type of $number in the leading $string. The str2int returns the stripped string data, naked of all integers, in and the array of floats For the ascii_float option, the members of the are scalar strings of the float numbers otherwise, the members are a reference to an array of where the decimal point is set so that there is one decimal digit to the left of the decimal point for $decimal_magnitude. $float = str2float($string, = str2float($string, = str2float subroutine, in an array context, supports converting multiple run of integers, decimals or floats in an array of strings to an array of integers, decimals or floats, It keeps converting the strings, starting with the first string in continuing to the next and next until it fails an conversion. The Data::Str2Num program module provides subroutines that parse numeric strings from the beginning of alphanumeric strings. ![]() ![]() $integer = $secspack->str2integer($string, = if a subroutine will process a list of options, that subroutine will also process an array reference, or hash reference, \%options, If a subroutine will process an array reference, that subroutine will also process a hash reference, \%options, See the description for a subroutine for details and exceptions. ![]() $float = $secspack->str2float($string, = $secspack->str2int($string) # For class interface, use Data::SecsPack instead of $self $integer = str2integer($string, = Class, Object interface $float = str2float($string, = $secspack->str2int($string) Use Data::Str2Num qw(config str2float str2int str2integer) Data::Str2Num - int str to int float str to float, else undef. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |