From: amirovic on
Hi,

I got two simple question where I don't know the answer and would
appreciate any help.

1) my $sting = "afdsf,sdgj,sdgjkgd,"
How can I find out how many commas are in this string. I think that
there should be a very simple perl solution to this without find it
out in a very wild way.

2) my $no = 0.00001;
print $no; // Output 1e-05
How can get 0.00001 and not an e-number?

Thanks for your help.
Regards,
Amir
From: nolo contendere on
On May 7, 2:08 pm, "amiro...(a)googlemail.com" <amiro...(a)googlemail.com>
wrote:
> Hi,
>
> I got two simple question where I don't know the answer and would
> appreciate any help.
>
> 1) my $sting = "afdsf,sdgj,sdgjkgd,"
> How can I find out how many commas are in this string. I think that
> there should be a very simple perl solution to this without find it
> out in a very wild way.

This is a FAQ:

$ perldoc -q count
H7JEWENMJ3(1) perl v5.8.3 (2008-05-07) H7JEWENMJ3(1)

Found in /usr/local/pkgs/perl-5.8.3/lib/5.8.3/pod/perlfaq4.pod
How can I count the number of occurrences of a substring
within a string?

There are a number of ways, with varying efficiency. If you
want a count of a certain single character (X) within a
string, you can use the "tr///" function like so:

$string = "ThisXlineXhasXsomeXx'sXinXit";
$count = ($string =~ tr/X//);
print "There are $count X characters in the string";

so for you, that would be:

$ perl -e 'my $string = "afdsf,sdgj,sdgjkgd,"; $count = ($string =~
tr/,//); print "$count commas\n"'
$ 3 commas


>
> 2) my $no = 0.00001;
> print $no; // Output 1e-05
> How can get 0.00001 and not an e-number?
>

try printf:

perl -e 'my $num = 0.00001; printf "%f", $num'
From: amirovic on
Hi,

Thank you very much for your great and fast response. For the second
question I needed to save the number without the 'e' in a further
variable which later was converted to a sting. But after your hint I
used sprintf which was made for this purpose.

Thanks again and regards,
Amir