Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Create an array from Optionmenu variables

by shortyfw06 (Beadle)
on Mar 12, 2012 at 23:33 UTC ( #959204=perlquestion: print w/ replies, xml ) Need Help??
shortyfw06 has asked for the wisdom of the Perl Monks concerning the following question:

The following code creates a specified number of optionmenus and stores the optionmenu data in variables. I need to use the stored data for several things. For example, I need to know the number of different combinations stored, ie. PW -45, PW 0, PW 45, PW 90, 8HS 0 = 5 combinations. I also need to know the number of different materials stored, ie. PW -45, PW 0, PW 45, PW 90, 8HS 0 = 2 different materials. Finally, I need to know the thickness of each different combination, ie. 2*(PW 0 0.0077)= 0.0154, etc. Can someone please help get me pointed in the right direction? <\p>

use Tk; use Cwd; ###################################################################### +##################### # GUI Building ###################################################################### +##################### # Create Main Window my $mw = new MainWindow; my $lam_num; my $row=1; my $column=1; my $lam_mat_frm = $mw -> Frame(); $lam_mat_frm->grid(-row=>$row, -column=>$column,-columnspan=>6); my $lam_num_lab = $lam_mat_frm -> Label(-text=>"Input the number of pl +ies in the laminate.", -font=>"ansi 10 bold"); $lam_num_lab->grid(-row=>$row, -column=>$column); $column++; my $lam_num_ent = $lam_mat_frm -> Entry(-textvariable=> \$lam_num); $lam_num_ent->grid(-row=>$row, -column=>$column); $column++; my $lam_data_button = $lam_mat_frm->Button(-text=>"Input Laminate Data +", -command=> \&input_lam_data); $lam_data_button->grid(-row=>$row, -column=>$column); MainLoop; sub input_lam_data { $row=4; $column=2; my @lam_mat_t_lab = ( 'Material', 'Thickness', 'Orientation', ); my $n = 1; for my $label (@lam_mat_t_lab) { $mat_lab{$n} = $lam_mat_frm -> Label (-text=> $label, -font=>"ansi 8 bold"); $mat_lab{$n}->grid(-row=>$row, -column=>$column); $column=$column+1; }; my %ply_lab; $n = 1; $row=5; do { $column = 1; $ply_lab{$n} = $lam_mat_frm -> Label (-text=>"Ply $n", -font=>"ansi 8 bold"); $ply_lab{$n}->grid(-row=>$row,-column=>$column); $row = $row+1; $n = $n+1; } until ($n == $lam_num+1); # Create Material Optionmenus my @mats = ("PW","8HS","Tape"); my %mat_optmen; $n = 1; $row=5; $column=2; do { $mat_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@mat +s, -variable => \$mat{$n}); $mat_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Thickness Optionmenus my @ts = ("0.0077","0.0147","0.0054"); my %t_optmen; $n = 1; $row=5; $column=3; do { $t_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@ts, -variable => \$t{$n}); $t_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Orientation Optionmenus my @orts = ("-45","0","45","90"); my %ort_optmen; $n = 1; $row=5; $column=4; do { $ort_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@ort +s, -variable => \$ort{$n}); $ort_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); my $print_button = $lam_mat_frm->Button(-text=>"Print BJSFM Input +File", -command=> \&print_input_file); $print_button->grid(-row=>$row,-column=>1); } sub print_input_file { }

Comment on Create an array from Optionmenu variables
Download Code
Re: Create an array from Optionmenu variables
by Anonymous Monk on Mar 13, 2012 at 05:45 UTC

    Can someone please help get me pointed in the right direction?

    Get rid of all Tk stuff, and define your problem in terms of subs and data structures :)

      Add this to the end of sub input_lam_data

      use Tk::ObjScanner; use Tk::WidgetDump; Tk::ObjScanner::scan_object( { q{\%ort_optmen} => \%ort_optmen , q{\%t_optmen} => \%t_optmen , q{\%ply_lab} => \%ply_lab , q{\%mat_optmen} => \%mat_optmen , } ); $mw->WidgetDump;

      Its diagnostical, see Tk::ObjScanner and Tk::WidgetDump

        I don't have ObjScanner.pm.

      I'm not sure I understand what you're saying?... Let's say I have the following

      my $n = 4; my $mat1 = "PW"; my $t1 = "0.0077"; my $ort1 = "45"; my $mat2 = "PW"; my $t2 = "0.0077"; my $ort2 = "45"; my $mat3 = "8HS"; my $t3 = "0.0147"; my $ort3 = "0"; my $mat4 = "PW"; my $t4 = "0.0077"; my $ort4 = "0";

      Now I want to sort it as described in the original post. How do I do this efficiently? My first thought would be to write several if statements but I imagine there must be a better way to go about this. Thanks for the help.

        I'm not sure I understand what you're saying?... Let's say I have the following

        Um, that is not an array, or three arrays, you can't sort that. I too am completely unsure of what you're asking about. Does this help?

        #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; Main( @ARGV ); exit( 0 ); sub Main { my @choice = Ply( 0, 0, 0 ); dd\@choice; push @choice, Ply( 1, 1, 1 ); push @choice, Ply( 2, 2, 3 ); dd\@choice; print "Combinations ", int( @choice ),"\n"; print "Materials ", CountMaterials( \@choice ),"\n"; } sub CountMaterials { my( $co ) = @_; my %seen; for my $item ( @$co ){ my $mat = $item->[0]; $seen{ $mat }++; } return scalar keys %seen; } BEGIN { my @Thickness = ( "0.0077","0.0147","0.0054"); my @Material = ( "PW","8HS","Tape"); my @Orientation = ("-45","0","45","90"); sub Ply { my( $mat, $thi, $ori ) = @_; return [ $Material[ $mat ], $Thickness[ $thi ], $Orientation[ $ori ], ]; } } __END__ [["PW", 0.0077, -45]] [["PW", 0.0077, -45], ["8HS", 0.0147, 0], ["Tape", 0.0054, 90]] Combinations 3 Materials 3

        All you have to do is write a function to query the target option menu and return an array like CountMaterials expects

Re: Create an array from Optionmenu variables
by zentara (Archbishop) on Mar 13, 2012 at 16:51 UTC
    I think you are running into difficulties because you don't have a decent data structure setup to hold the selections. You have a hodgepodge of local and global hashes and variables which are cumbersome to collect and sort. You might be better off reorganizing your data into a better hash which you can more easily loop thru.

    Furthermore you did not use strict and use warnings as I mentioned to you before in Re^3: Optionmenu Variable in Name. This programming negligence of yours causes us to have to track down all sorts of bugs in your code. Same thing again here. If you add strict and warnings, you get

    Global symbol "%mat_lab" requires explicit package name at ./959204.pl + line 41. Global symbol "%mat_lab" requires explicit package name at ./959204.pl + line 43. Global symbol "%mat" requires explicit package name at ./959204.pl lin +e 66. Global symbol "%t" requires explicit package name at ./959204.pl line +80. Global symbol "%ort" requires explicit package name at ./959204.pl lin +e 94. Execution of ./959204.pl aborted due to compilation errors.
    This is important, because you treat %t, %ort, and %mat like they are globals but they are NOT. So how can you access them in your sub print_input_file?

    A fixed version of your code shows how to get that sub to print your values.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Cwd; ###################################################################### +##################### # GUI Building ###################################################################### +##################### # make the selections available to the print_input_values sub my %t; my %mat; my %ort; my %mat_lab; # Create Main Window my $mw = new MainWindow; my $lam_num; my $row=1; my $column=1; my $lam_mat_frm = $mw -> Frame(); $lam_mat_frm->grid(-row=>$row, -column=>$column,-columnspan=>6); my $lam_num_lab = $lam_mat_frm -> Label(-text=>"Input the number of pl +ies in the laminate.", -font=>"ansi 10 bold"); $lam_num_lab->grid(-row=>$row, -column=>$column); $column++; my $lam_num_ent = $lam_mat_frm -> Entry(-textvariable=> \$lam_num); $lam_num_ent->grid(-row=>$row, -column=>$column); $column++; my $lam_data_button = $lam_mat_frm->Button(-text=>"Input Laminate Data +", -command=> \&input_lam_data); $lam_data_button->grid(-row=>$row, -column=>$column); MainLoop; sub input_lam_data { $row=4; $column=2; my @lam_mat_t_lab = ( 'Material', 'Thickness', 'Orientation', ); my $n = 1; for my $label (@lam_mat_t_lab) { $mat_lab{$n} = $lam_mat_frm -> Label (-text=> $label, -font=>"ansi 8 bold"); $mat_lab{$n}->grid(-row=>$row, -column=>$column); $column=$column+1; }; my %ply_lab; $n = 1; $row=5; do { $column = 1; $ply_lab{$n} = $lam_mat_frm -> Label (-text=>"Ply $n", -font=>"ansi 8 bold"); $ply_lab{$n}->grid(-row=>$row,-column=>$column); $row = $row+1; $n = $n+1; } until ($n == $lam_num+1); # Create Material Optionmenus my @mats = ("PW","8HS","Tape"); my %mat_optmen; $n = 1; $row=5; $column=2; do { $mat_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@mat +s, -variable => \$mat{$n}); $mat_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Thickness Optionmenus my @ts = ("0.0077","0.0147","0.0054"); my %t_optmen; $n = 1; $row=5; $column=3; do { $t_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@ts, -variable => \$t{$n}); $t_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Orientation Optionmenus my @orts = ("-45","0","45","90"); my %ort_optmen; $n = 1; $row=5; $column=4; do { $ort_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@ort +s, -variable => \$ort{$n}); $ort_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); my $print_button = $lam_mat_frm->Button(-text=>"Print BJSFM Input +File", -command=> \&print_input_file); $print_button->grid(-row=>$row,-column=>1); } sub print_input_file { print "$lam_num\n"; my @values; foreach my $n (1.. ($lam_num) ){ push @values, $mat{$n}; push @values, $t{$n}; push @values, $ort{$n}; } print join ",", @values,"\n"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://959204]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-12-29 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls