sanjay nayak has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to extract the first elemnet from an integer type value ???
by b10m (Vicar) on Aug 31, 2006 at 08:58 UTC
    print substr($num, 0, 1);

    See substr

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: How to extract the first elemnet from an integer type value ???
by borisz (Canon) on Aug 31, 2006 at 09:07 UTC
    or use a regex
    print $num =~ /^(.)/;
    Boris
Re: How to extract the first elemnet from an integer type value ???
by GrandFather (Saint) on Aug 31, 2006 at 09:22 UTC
    my $digit = (split //, '100')[0];

    Update or

    my $num = 100; my $digit = int ($num / 10 ** int (log ($num) / log(10)));

    DWIM is Perl's answer to Gödel
      my $num = 100; my $digit = int ($num / 10 ** (length($num)-1) );
      --
      Leviathan
        my $num = 100; my $digit = int ($num / ('1' . ('0' x int (length ($num) -1))));

        DWIM is Perl's answer to Gödel
Re: How to extract the first elemnet from an integer type value ???
by prasadbabu (Prior) on Aug 31, 2006 at 09:18 UTC
    TIMTOWTDI $num = 100; print unpack('A', $num);

    Prasad

Re: How to extract the first elemnet from an integer type value ???
by vi_srikanth (Acolyte) on Aug 31, 2006 at 09:02 UTC
    print int ($num / 100); or print substr($num,0,1); or if ($num=~m/(.)/) {print $1}
Re: How to extract the first elemnet from an integer type value ???
by bart (Canon) on Aug 31, 2006 at 20:59 UTC
    People are giving you lots of little recipes, without explaining how they work. The basic idea is this:
    In Perl, you can just use a number in a scalar as a string.
    Perl will convert it for you, from number to string, if necessary. So, all you have to do is extract the first character from the number treated as a string. That's what substr, unpack, the regex /^(.)/ all do.
Re: How to extract the first elemnet from an integer type value ???
by GrandFather (Saint) on Aug 31, 2006 at 10:11 UTC
    $num -= 1;

    DWIM is Perl's answer to Gödel
Re: How to extract the first elemnet from an integer type value ???
by davido (Cardinal) on Aug 31, 2006 at 17:03 UTC
    print reverse($num)+0;

    Dave

      Perhaps you meant this?

      print reverse($num) % 10;

      Pax vobiscum et gratias vobis ago valeteque,
      -v

      "Perl. There is no substitute."

        No, I meant print reverse($num)+0;, which extracts '1' from the integer 100, as the OP asked. He didn't ask about 52, 583, 47, or 32767, he asked about 100. ;)

        Yours works too, of course. The difference being, yours works over any integer, and mine works for the integer 100, or any other integer where the leading digit contains a positive value and the subsequent digits are zero. But both extract 1 from 100.

        He may also have meant all integers, or he may also have meant any integer with one leading significant digit and all trailing digits being zero. Or he may have meant only the integer 100, or maybe an integer beginning with 1 and followed by only zeros, or maybe he meant only positive integers, or maybe something else entirely, but he said an integer, 100. Any other assertion is guesswork.

        Of course we're just having fun. Everyone knows the best way is this:

        print shift @{[(split//,$num)[0-(@{[split//,$num]})]]}
Re: How to extract the first elemnet from an integer type value ???
by mreece (Friar) on Aug 31, 2006 at 18:53 UTC
    in the spirit of reverse-golf silly answers:
    my $num = 100; my $extracted = do { open 0 and $_ = ( grep /my \$num = \d/, <0> )[0] +and /(\d)/; $1; };
Re: How to extract the first elemnet from an integer type value ???
by swampyankee (Parson) on Aug 31, 2006 at 21:05 UTC
    my $num = 100; print "First digit of $num is " . int($num / (10 ** int(log(abs($num)) +/log(10)))) . "\n";

    Make sure that some code is wrapped about it to make sure that you don't try to take log(0). log(10) is there because log is actually loge, and log10 is needed.

    emc

    Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.

    Albert Einstein