in reply to Re: converting binary to decimal
in thread converting binary to decimal

Perl says, "Bareword found where operator expected," and it's pointing to the regex on this line:

my $decimal = (join '', map sprintf('%0*d', $size, $_), reverse @number) =~ s/0+(?=.)//r;

I don't know why I'm getting this error, and don't know how to fix it, because honestly, I don't even understand what this line does. I tried to remove this line entirely from the code and just "print join('-', @number);" but all I see is a list of very big numbers... and I don't even understand how the algorithm is supposed to work. In other words, I am completely lost at this one.

Replies are listed 'Best First'.
Re^3: converting binary to decimal
by ysth (Canon) on Jun 26, 2025 at 19:23 UTC
    You seem to be running an extremely old version of perl. Try changing it it:
    (my $decimal = join '', map sprintf('%0*d', $size, $_), reverse @numbe +r) =~ s/0+(?=.)//;
Re^3: converting binary to decimal
by hippo (Archbishop) on Jun 26, 2025 at 17:59 UTC
    Perl says, "Bareword found where operator expected,"

    Use a version of Perl released in the last 15 years and this problem does not occur.


    🦛

      Okay. I do use a modern Perl when I am on Linux. But when I am on Windows, I like to use TinyPerl.
Re^3: converting binary to decimal
by tybalt89 (Monsignor) on Jun 26, 2025 at 19:41 UTC

    replace

    my $decimal = (join '', map sprintf('%0*d', $size, $_), reverse @numbe +r) =~ s/0+(?=.)//r;

    with

    s/0+(?=.)// for my $decimal = join '', map sprintf('%0*d', $size, $_), + reverse @number;

    It's just doing math in base $n which is 10**18 in this case (because that's the largest power of 10 that works on my machine). Change $size to 1 to see it work (more slowly) in base 10.

    It's doing the same kind of thing that 'bigint' does...

      The output I'm getting looks like this. And I don't know why. I tried both suggestions, and it's the same with both:

      000000001-00000000000000001-00000000000000001-00000000000000001-000000 +0000000000 1-00000000000000001-00000000000000001-00000000000000001-00000000000000 +001-000000 00000000001-00000000000000001-00000000000000001-00000000000000001-0000 +0000000000 001-00000000000000001-00000000000000001-00000000000000001-000000000000 +00001-0000 0000000000001-00000000000000001-00000000000000001-00000000000000001-00 +0000000000 00001-00000000000000001-00000000000000001-00000000000000001-0000000000 +0000001-00 000000000000001-00000000000000001-00000000000000001-00000000000000001- +0000000000 0000001-00000000000000001-00000000000000001-00000000000000001-00000000 +000000001- 00000000000000001-00000000000000001-00000000000000001-0000000000000000 +1-00000000 000000001-00000000000000001-00000000000000001-00000000000000001-000000 +0000000000 1-00000000000000001-00000000000000001-00000000000000001-00000000000000 +001-000000 00000000001-00000000000000001-00000000000000001-00000000000000001-0000 +0000000000 001-00000000000000001-00000000000000001-00000000000000001-000000000000 +00001-0000 0000000000001-00000000000000001-00000000000000001-00000000000000001-00 +0000000000 00001-00000000000000001-00000000000000001-00000000000000001-0000000000 +0000001-00 000000000000001-00000000000000001-00000000000000001-00000000000000001- +0000000000 0000001-00000000000000001-00000000000000001-00000000000000001-00000000 +000000001- 00000000000000001-00000000000000001-00000000000000001-0000000000000000 +1-00000000 000000001-00000000000000001-00000000000000001-00000000000000001-000000 +0000000000 1-00000000000000001-00000000000000001-00000000000000001-00000000000000 +001-000000 00000000001-00000000000000001-00000000000000001-00000000000000001-0000 +0000000000 001-00000000000000001-00000000000000001-00000000000000001-000000000000 +00001-0000 0000000000001-00000000000000001-00000000000000001-00000000000000001

        <fightingsarcasm>It would be helpful to see the code that produces this output.</fightingsarcasm>