2 simple questions

Discussion in 'Perl Misc' started by amirovic@googlemail.com, May 7, 2008.

  1. Guest

    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
     
    , May 7, 2008
    #1
    1. Advertising

  2. On May 7, 2:08 pm, "" <>
    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'
     
    nolo contendere, May 7, 2008
    #2
    1. Advertising

  3. Guest

    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
     
    , May 7, 2008
    #3
  4. Achim Peters Guest

    schrieb:

    > 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
     
    Achim Peters, May 8, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ali Syed
    Replies:
    3
    Views:
    584
    Mark McIntyre
    Oct 13, 2004
  2. pkirk25
    Replies:
    50
    Views:
    1,169
    Bill Pursell
    Oct 4, 2006
  3. Replies:
    10
    Views:
    583
  4. Vijay
    Replies:
    2
    Views:
    664
    Öö Tiib
    May 7, 2010
  5. reema
    Replies:
    0
    Views:
    313
    reema
    Aug 26, 2008
Loading...

Share This Page