wrote:
> I'm writing a small Gtk2 program to convert between bases. I'm getting
> a strange error when i run my code:
>
> Use of uninitialized value in exponentiation (**) at ./radix line 216.
> Use of uninitialized value in exponentiation (**) at ./radix line 190.
>
> The strange thing is, sometimes I get it, sometimes I don't. Anyone
> have any insight into what's causing it?
Yes, you are using an overly complex algorithm to do something that Perl
provides a function to do for you.
> Second question. How do you append text to a textbuffer without
> overwriting it?
Use the concatenation operator:
perldoc perlop
> Also, while I'm here, I wouldn't mind some constructive criticism on my
> coding style. Any input would be appreciated.
>
> Here is the offending code.
>
> #!/usr/bin/perl -w
>
> use strict;
>
> [snip]
>
> #-------------Main Conversion Routine-------------#
> sub input_callback{
> my $dec = $entry->get_text();
> my $virgin = $dec;
> my @binary;
>
> if ($dec==0){
> unshift @binary,0;
>
> }
> else{
> while ($dec >= 1 ){
>
> if ($dec==0){
> unshift @binary,0;
> last;
> }
> if ($dec==1){
> unshift @binary,1;
> $dec = $dec / 2;
> last;
> }
> if ($dec % 2){
> unshift @binary, 1;
> $dec = $dec / 2;
> }
> else{
> unshift @binary, 0;
> $dec = $dec / 2;
> }
>
>
> }#while
> }#top else
> $radixFrameHash{'Binary:'}->set_text("@binary");
> $radixFrameHash{'Decimal:'}->set_text("$virgin");
> $radixFrameHash{'Octal:'}->set_text(&bin2oct(@binary));
> $radixFrameHash{'Hex:'}->set_text(&bin2hex(@binary));
> $radixFrameHash{'ASCII:'}->set_text(chr $virgin);
> #$text_view->set_buffer ("converting decimal number to binary");
>
> }#input callback
> #-------------Main Conversion Routine-------------#
#-------------Main Conversion Routine-------------#
sub input_callback{
my $dec = $entry->get_text();
$radixFrameHash{'Binary:'}->set_text( sprintf '%#b', $dec );
$radixFrameHash{'Decimal:'}->set_text( $dec );
$radixFrameHash{'Octal:'}->set_text( sprintf '%#o', $dec );
$radixFrameHash{'Hex:'}->set_text( sprintf '%#x', $dec );
$radixFrameHash{'ASCII:'}->set_text( chr $dec );
#$text_view->set_buffer ("converting decimal number to binary");
}
#-------------Main Conversion Routine-------------#
perldoc -f sprintf
John
--
use Perl;
program
fulfillment