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

Esteemed monks,

I have had a similar problem to this before, and I am still not sure how to fix it in this case.

@map_fields is declared as a global variable. It isused to hold the variable from each of a number of BrowseEntry's which are created like this:

$cnt2 = 0; my $dbfields = $dbh->selectcol_arrayref ("SHOW COLUMNS FROM lead") +; push ( @$dbfields, 'SKIP'); foreach my $field ( @file_fieldnames ) { $pg->Label(-relief => 'sunken', -width => 15, -text => $field) +->grid(my $brb = $pg->BrowseEntry(-variable => \$map_fields[$cnt2])); $brb->insert('end', @$dbfields); $cnt2++; }
My goal is to map the fields that are in a text file I am importing (which are variable in number and in order), to the fields in the database table. The array referenced by $dbfields gets a list of the fields in the table. When it is selected it is saved in the appropriate slot in @map_fields.

I have a db table which also stores preset configurations (user can also store his own). When I load a configuartion from the database and 'split' the string into the array like this:

@map_fields = split ( ",", $ifg->{parameter} );
The string in $ifg->{parameter} is a comma delimited string containg the field names that get split into @map_fields.

Then I do not see the new values in the BrowseEntry's. The @map_fields array can be used to do the field mapping alright (I use the two array's to build an SQL query that ALTER's the field names in a temp table, then I append the resulting table to the table I want to import the file into).

Can anyone offer a suggestion as to how I get the new values to show up in the BrowseEntry's??

Your help is much appreicated.

jdtoronto

Replies are listed 'Best First'.
Re: Updating variables in Tk programmes
by graff (Chancellor) on Sep 03, 2003 at 07:57 UTC
    Did you make a pasting mistake when posting your code, or are you really trying to have the "grid()" call for "$pg->Label()" include the creation of the BrowseEntry widget? That makes no sense. I would expect the for loop to look something like this:
    my ($row,$mfld) = (0,0); for my $field ( @file_fieldnames ) { # (and where does this array come + from?) $pg->Label(....)->grid(-row => $row, -column => 0); my $brb = $pg->BrowseEntry(-variable => \$map_fields[$mfld++], )->grid(-row => $row, -column => 1); $brb->insert('end', @$dbfields); $row++; }
    There may still be more things wrong with this, depending on what the @file_fieldnames array is supposed be... Why would you put the same set of field names into several BrowseEntry widgets?
Re: Updating variables in Tk programmes
by bobn (Chaplain) on Sep 03, 2003 at 10:08 UTC

    I'm probably going to mess this up, but I think what's happening is:

    You use \$map_fields[$cnt2] to store the location of (pointer to) the space occupied by $map_fields[$cnt2], but I think that the dereferencing of the array is happening first, so you store a reference to a string which is the value stored at $map_fields[$cnt2].

    I think the solution is to initially store references to scalars in @map_fields, then always manipulate them as references, as in:
    use Tk; $mw = new MainWindow(-title=>'mapp'); $pg = $mw->Frame; @map_fields = \(qw( A B C D E )); $cnt2 = 0; @file_fieldnames = qw( SHOW COLUMNS FROM lead SKIP ); foreach my $field ( @file_fieldnames ) { $pg->Label(-relief => 'sunken', -width => 15, -text => $field)->pac +k(); my $brb = $pg->BrowseEntry(-variable => ($map_fields[$cnt2]) ); $brb->insert('end', @$dbfields); $cnt2++; $brb->pack(); } my $btn =$pg->Button(-text=>'doit', -command => sub { doit() } ); $btn->pack; $pg->pack(); MainLoop; sub doit { $ifg->{parameter} = '1,2,3,4,5'; my @a = ( split ( ",", $ifg->{parameter} ) ); for ( @map_fields ) { $$_ = shift @a } ; }
    This code seems to do what you want, though I didn't learn the grid placment manager or make a DB to truly try it out.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.