in reply to Re: Printing to stdout an array of strings and scalar references
in thread Printing to stdout an array of strings and scalar references

Well, I can say I learned something today... I had no idea that you could separate integers with underscores to make it easier at a glance how long the number actually is.

perl -E 'say 500_000' 500000

Cool!

  • Comment on Re^2: Printing to stdout an array of strings and scalar references
  • Download Code

Replies are listed 'Best First'.
Re^3: Printing to stdout an array of strings and scalar references
by kcott (Archbishop) on Oct 26, 2017 at 08:27 UTC

    You can use underscores with decimal, hexadecimal, octal and binary numbers. You can also place them wherever you want, for convenient viewing of the data in question. They don't have to be a direct replacement of commas: both 50_00_00 and 500_000 are valid representations of 500,000; although, the first one is probably a poor choice because it's likely to be confusing.

    Here's some highly contrived examples (purely to demonstrate those points):

    $ perl -E 'say 1_6' 16 $ perl -E 'say 0x1_0' 16 $ perl -E 'say 02_0' 16 $ perl -E 'say 0b1_00_00' 16

    See "perldata: Scalar value constructors" for details.

    — Ken

      both 50_00_00 and 500_000 are valid representations of 500,000; although, the first one is probably a poor choice because it's likely to be confusing.

      Not everyone on earth seperates digits in groups of three. In India, it is common to use groups of two and groups of three, mixed: Indian numbering system.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Curiously, I was actually thinking of that when composing my post. I thought their non-standard grouping was four; so five hundred thousand, 500_000, would be fifty myriad, 50_0000 (although they have a different word for myriad).

        Anyway, the point was that you can put the underscores wherever you want (so long as you don't put two or more together). And the examples were already getting almost to the point of silliness (e.g. 16 as 1_6), without embarking on a world-tour of numbering systems.

        Thanks for the link: I did have a quick look; I was wrong about the myriad grouping; it's been about 15 years since I last encountered that, so I'm not overly surprised that it wasn't fresh in my memory.

        — Ken

Re^3: Printing to stdout an array of strings and scalar references
by eyepopslikeamosquito (Archbishop) on Oct 25, 2017 at 20:44 UTC

    Yes, I'm also a fan of Perl allowing underscores in numeric literals.

    BTW, I was pleasantly surprised to learn that the C++ committee are also fans of this splendid idea:

    • C++11 user-defined literals: These are extensible and use a trailing underscore. For example: 12_km
    • C++14 digit separators: "The single-quote character ' can now be used anywhere within a numeric literal for aesthetic readability". For example: 500'000 or 500'000_km or pi = 3.14159'26535'89793. Presumably, single quote was chosen for the digit separator because underscore had already been taken by user-defined literals.

Re^3: Printing to stdout an array of strings and scalar references
by Your Mother (Archbishop) on Oct 25, 2017 at 17:23 UTC

    In fact, it's a perlism I am extremely fond of. I can easily see someone in a design discussion saying, What? We don't need that informal, random parsing trouble. It's just numbers. If you can't read numbers, you shouldn't be programming. That underscore has saved me many hours of reading comprehension and probably a hundred hours of chasing bugs and mistakes in code. Update: and scratches on my monitor as I try to count digits with pen or ruler. :P