Loading ...
Sorry, an error occurred while loading the content.

Re: dc tricks with Perl

Expand Messages
  • Paul Archer
    Any suggestions here? Paul ... Q: What do Winnie-the-Pooh and John the Baptist have in common? A: Their middle name.
    Message 1 of 11 , Feb 2, 2006
    • 0 Attachment
      Any suggestions here?

      Paul

      Tuesday, Paul Archer wrote:

      >> try this at the prompt
      >>
      >>
      >> echo 49121642460476302043559655325609735240131249408713384930308013834P
      >> | dc
      >>
      >
      > This brings us back to Perl, with my attempt at a 'stupid DC tricks
      > generator':
      >
      > The problem is that strings longer than about eight characters produce values
      > that get represented in exponential notation. I've tried 'use integer', which
      > limits the size that the variable can be, and I've played around with printf
      > to no good effect.
      > So, how do you create really, really large integers in Perl without them
      > getting truncated, or binding C functions?
      >
      > echo 345232469002P | dc
      >
      >

      > #!/usr/bin/perl
      > use warnings;
      > use strict;
      > #use integer;
      >
      > print "gimme a string: ";
      > my $string=<STDIN>;
      > # no chomp 'cause we want the newline for the dc output
      > print "string is $string";
      >
      > our $total=0;
      >
      > foreach my $char (split //, $string){
      >
      > $total *=256;
      > $total += ord($char);
      > }
      >
      > print "total is $total\n";
      > printf "echo %luP | dc\n", $total;


      ----------------------------------------------------------------
      Q: What do Winnie-the-Pooh and John the Baptist have in common?
      A: Their middle name.
      ----------------------------------------------------------------
    Your message has been successfully submitted and would be delivered to recipients shortly.