2 simple questions

A

amirovic

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
 
N

nolo contendere

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'
 
A

amirovic

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
 
A

Achim Peters

For the second
question I needed to save the number without the 'e' in a further
variable which later was converted to a sting.

If it was only _later_ converted to string, then _before_ that it was
still a number in your "further variable", right? But having or not
having an "e" is not a property of a number but rather of its printed
representation only. So until you actually convert the number to a
string later, it's meaningless whether it's "with" or "without the 'e'".
JFTR.

Bye
Achim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top