2 simple questions

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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.

Re: 2 simple questions

Quoted text here. Click to load it

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

Quoted text here. Click to load it

try printf:

perl -e 'my $num = 0.00001; printf "%f", $num'

Re: 2 simple questions


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,

Re: 2 simple questions

amirovic@googlemail.com schrieb:

Quoted text here. Click to load it

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'".


Site Timeline