CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
STRINGS

 



yaz
stranger

Aug 16, 2001, 7:35 AM

Post #1 of 5 (428 views)
STRINGS Can't Post

How do you get the length of a string.
I want to write a function that when you give it a numerical string eg "150000000" it will return it as "150,000,000". Ie the output will be formatted with the commas. Any ideas?

Many Thanx



wickedxter
User

Aug 16, 2001, 9:34 AM

Post #2 of 5 (426 views)
Re: STRINGS [In reply to] Can't Post

use:
$string = length($number);

$string should hold how many char. are in $number

then use :
if($string eq "9"){
substr($number, 3, 0) = ",";
substr($number, 8, 0) = ",";
}
print $number;

then $number should turn out like 100,000,000

I know there is a simpler way of doing this but im not sure of how to, maybe someone will and post it. You might wanna mess with the substr till u get it right

substr($string, skip_char, length) = "";
$blah = $substr($string, $skip_char, $length);



fashimpaur
User / Moderator

Aug 16, 2001, 10:40 AM

Post #3 of 5 (425 views)
Re: STRINGS [In reply to] Can't Post

Yaz,

Try this:

use English;
my $input = "150000.12345";
$input = int $input; # formatting only works on integer values
my $output = '';

while (length($input) > 0){
$output = ','.$output if $output ne ''; # precede output with a comma if output is not empty string
$input =~ /(.{1,3})$/; # find last 1 (min) to 3(max) characters
$output = $MATCH.$output; # prepend match to output
$input = $PREMATCH ne '' ? $PREMATCH : ''; # if prematch is not empty, input = prematch else it is empty
}

print $output;

HTH,

Dennis

$a="c323745335d3221214b364d545a362532582521254c3640504c37292f493759214b3635554c3040606a0",print unpack"u*",pack "h*",$a,"\n\n"


mhx
Enthusiast / Moderator

Aug 16, 2001, 11:48 AM

Post #4 of 5 (423 views)
Re: STRINGS [In reply to] Can't Post

Give this subroutine a try:

Code
sub FormatValue ($) { 
my $v = reverse shift;
$v =~ s/(\d{3})(?=\d+$)/$1,/g;
scalar reverse $v;
}

It works for arbitrary length integer and floating point numbers. For example,

Code
print FormatValue '1234567890123.12456789', "\n"; 
print FormatValue '1234567890123', "\n";

will print

Code
1,234,567,890,123.12456789 
1,234,567,890,123

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



yaz
stranger

Aug 17, 2001, 8:27 AM

Post #5 of 5 (415 views)
Re: STRINGS [In reply to] Can't Post

Thanx ppl.
i have now got it working

rgds
yaz


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives