Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've been working on a program for sometime now and while working on the finishing part I receive an error while trying to display text that I retrieved from a Text widget.

I have ten text fields and all of them work on having their contents retrieved and displayed from them except one which is exactly the same as the others. I've checked it a million times.. It's kind of hard to explain the whole situation so I cut down the program and made a new one based just on that area. I'm also using ActivePerl 5.6.1 (Build 633) on Windows XP.

use Tk; use strict; use warnings; #--- Variables--- my $program_title = "TESTING"; my $admission_1_value; my $admission_2_value; my $admission_3_valu +e; my $admission_4_value; my $admission_5_value; my $admission_6_value; my $ +admission_7_value; my $admission_8_value; my $admission_9_value; my $admission_10_value; +my $charge_1_value; my $charge_2_value; my $charge_3_value; my $charge_4_value; my $charge +_5_value; my $charge_6_value; my $charge_7_value; my $charge_8_value; my $charge_9_ +value; my $charge_10_value; #--- Define Main Window --- my $main_window = MainWindow->new(); $main_window->configure(-background=>'#FFFFFF'); $main_window->title("$program_title"); $main_window->minsize(qw(250 450)); $main_window->geometry('+325+150'); #--- Start Bottom Processing Field my $frame_process = $main_window->Frame(-background=>'#FFFFFF')->p +ack(side=>'bottom'); $frame_process->Button( -text=>'Process', -command=>\&PROCESS)->pack(side=>'top'); #--- Start Admission Fields --- my $frame_names = $main_window->Frame(-background=>'#FFFFFF')->pac +k(side=>'left'); my $admission_10 = $frame_names->Text(-width=>20,-height=>1)->p +ack(side=>'bottom'); $frame_names->Label(-text=>'Admission # 10', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_9 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 9', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_8 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 8', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_7 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 7', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_6 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 6', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_5 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 5', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_4 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 4', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_3 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 3', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_2 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 2', + -background=>'#FFFFFF')->pack(side=>'bottom'); my $admission_1 = $frame_names->Text(-width=>20,-height=>1)->pack( +side=>'bottom'); $frame_names->Label(-text=>'Admission # 1', + -background=>'#FFFFFF')->pack(side=>'bottom'); #--- BLANK COLUMN my $frame_blank5 = $main_window->Frame(-background=>'#FFFFFF')- +>pack(side=>'left'); $frame_blank5->Label(-text=>' ', -background=>'#FFFFFF')->pack(side=>'bottom'); #---Start Charge Fields my $frame_charge = $main_window->Frame(-background=>'#FFFFFF')- +>pack(side=>'left'); my $charge_10 = $frame_charge->Text(-width=>12, -height=>1)->pack( +side=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_9 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_8 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_7 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_6 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_5 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_4 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_3 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_2 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); my $charge_1 = $frame_charge->Text(-width=>12, -height=>1)->pack(s +ide=>'bottom'); $frame_charge->Label(-text=>'Charge', -background=>'#FFFFFF')->pack(side=>'bottom'); sub PROCESS { #--- GET all Text Field Entries --- my $admission_1_value = $admission_1->get('1.0', 'end'); my $admission_2_value = $admission_2->get('1.0', 'end'); my $admission_3_value = $admission_3->get('1.0', 'end'); my $admission_4_value = $admission_4->get('1.0', 'end'); my $admission_5_value = $admission_5->get('1.0', 'end'); my $admission_6_value = $admission_6->get('1.0', 'end'); my $admission_7_value = $admission_7->get('1.0', 'end'); my $admission_8_value = $admission_8->get('1.0', 'end'); my $admission_9_value = $admission_9->get('1.0', 'end'); my $admission_10_value = $admission_10->get('1.0', 'end'); my $charge_1_value = $charge_1->get('1.0', 'end'); my $charge_2_value = $charge_2->get('1.0', 'end'); my $charge_3_value = $charge_3->get('1.0', 'end'); my $charge_4_value = $charge_4->get('1.0', 'end'); my $charge_5_value = $charge_5->get('1.0', 'end'); my $charge_6_value = $charge_6->get('1.0', 'end'); my $charge_7_value = $charge_7->get('1.0', 'end'); my $charge_8_value = $charge_8->get('1.0', 'end'); my $charge_9_value = $charge_9->get('1.0', 'end'); my $charge_10_value = $charge_10->get('1.0', 'end'); #-- Define Column Arrays --- my @patients = ("$admission_1_value", "$admission_2_value", "$admi +ssion_3_value", "$admission_4_value", "$admission_5_value", "$admission_6_ +value", "$admission_7_value", "$admission_8_value", "$admission_9_ +value", "$admission_10"); my @charge = ("$charge_1_value", "$charge_2_value", "$charge_3_val +ue", "$charge_4_value", "$charge_5_value", "$charge_6_value", + "$charge_7_value", "$charge_8_value", "$charge_9_value", + "$charge_10_value"); my @INVOICE = ("@patients", "\n", "@charge"); #--- PRINT ALL ENTRIES print @INVOICE; }; #--- Create GUI -- MainLoop;

As you can see when you run that code that if you fill up all the fields with text or anything, they all work except Admission #10. It instead prints "Tk::Text=HASH(0x2477ce0)". I've been trying for hours on what could be goin wrong but haven't had any success =\

If anybody could please lead me in the in right direction on this, it would be greatly appreciated! Thanks. =)

janitored by ybiC: Balanced <readmore> tags around long codeblock

Replies are listed 'Best First'.
Re: Unexpected Error While Retreiving Text in Perl\Tk
by saskaqueer (Friar) on Jun 08, 2004 at 06:38 UTC

    Here's a slightly revamped solution to reduce code redundancy. Anytime you see yourself using variables with digits in them ($charge_1_value, $charge_2_value, etc), what you really want is an array. It saved you much typing space. Here's a revised version of your code:

    #!perl -w use strict; use Tk; my $prog_title = 'TESTING'; my $mw = MainWindow->new(); $mw->configure(-background=>'#FFFFFF'); $mw->title($prog_title); $mw->minsize(qw(250 450)); $mw->geometry('+325+150'); my $frame_process = $mw->Frame( -background => '#FFFFFF' )->pack(-side => 'bottom'); $frame_process->Button( -text => 'Process', -command => \&process )->pack(-side => 'top'); my $frame_names = $mw->Frame( -background => '#FFFFFF' )->pack(-side => 'left'); my %fields; for my $index ( 1..10 ) { unshift( @{ $fields{'admission'} }, $frame_names->Text( -width => 20, -height => 1 )->pack(-side => 'bottom') ); $frame_names->Label( -text => 'Admission # ' . (11 - $index), -background => '#FFFFFF' )->pack(-side => 'bottom'); } my $frame_blank = $mw->Frame( -background => '#FFFFFF' )->pack(-side => 'left'); $frame_blank->Label( -text => ' ', -background => '#FFFFFF' )->pack(-side => 'bottom'); my $frame_charge = $mw->Frame( -background => '#FFFFFF' )->pack(-side => 'left'); for my $index ( 1..10 ) { unshift( @{ $fields{'charge'} }, $frame_charge->Text( -width => 12, -height => 1 )->pack(-side => 'bottom') ); $frame_charge->Label( -text => 'Charge', -background => '#FFFFFF' )->pack(-side => 'bottom'); } sub process { my @patients = map { $_->get('1.0', 'end') } @{ $fields{'admission +'} }; my @charge = map { $_->get('1.0', 'end') } @{ $fields{'charge'} }; my @invoice = (@patients, "\n", @charge); print @invoice; } MainLoop;
Re: Unexpected Error While Retreiving Text in Perl\Tk
by saskaqueer (Friar) on Jun 08, 2004 at 06:15 UTC

    In sub PROCESS, you accidentally put $admission_10 into @patients instead of $admission_10_value.

Re: Unexpected Error While Retreiving Text in Perl\Tk
by mawe (Hermit) on Jun 08, 2004 at 06:18 UTC
    Hi!

    If you change this
    my @patients = ("$admission_1_value", "$admission_2_value", "$admissio +n_3_value", ... "$admission_10");
    to
    my @patients = ("$admission_1_value", "$admission_2_value", "$admissio +n_3_value", ... "$admission_10_value");
    ... it should work. Hope this helps.
    mawe
Re: Unexpected Error While Retreiving Text in Perl\Tk
by eXile (Priest) on Jun 08, 2004 at 06:28 UTC
    Hi, just some random comments that came up while trying to debug your problem. Others already found your problem, but maybe this is useful as well:
    • pack(side=>'top') doesn't work on my computer, I had to change this to pack(-side=>'top')
    • Why don't you use Tk::Entry instead of Tk::Text? Tk::Entry is more lightweight than Tk::Text and offers everything you need in this code.
      pack(side=>'top') doesn't work on my computer, I had to change this to pack(-side=>'top')
      To explain this: Tk804.027 removed the deprecated usage of non-dashed options.
Re: Unexpected Error While Retreiving Text in Perl\Tk
by antioch (Sexton) on Jun 08, 2004 at 06:59 UTC
    Hmm.. When I first started I thought there would be some way to do it with an array but nevere really bothered to look. That saves a lot of space and time.

    eXile - Didn't know about Tk::Entry, I'll definately look into using it..
Re: Unexpected Error While Retreiving Text in Perl\Tk
by antioch (Sexton) on Jun 08, 2004 at 06:27 UTC
    *smacks forehead* grr, can't believe I didn't notice that haha. Well thanks a lot to both of you for pointing out my typo!

    -antioch

    (oh yeah, I accidently forgot to log in. Thats why its under anonymous monk)