Foreach looping an array in perl tk.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lashaw
    New Member
    • Mar 2010
    • 8

    Foreach looping an array in perl tk.

    This program is suppose to only display one name and number after the buttom is pressed. But the problem is it only displays the first name and number after the button is pressed. It won't display any other names or numbers but the first line. The program is not reading each line one at a time per button press. Only continue to display the first line in the file after the button is pressed. All help will be graceful!

    [code=perl tk]
    #!/usr/bin/perl -w
    require Tk;
    use Tk ':eventtypes';
    use Tk;

    my $mw = MainWindow->new();

    open (FILE, "new.txt") || die "Can't open File.txt: $!\n";
    $raw_data = <FILE>;

    foreach $_ ($raw_data)
    {
    chomp ($_);
    ($c_name, $desricpt_info) = split(/\|/, $_);
    $mw->Button (-text=>"two strings",
    -command=>[\&printstrings, $_])
    ->pack(-side=>"left");}
    $mw->Label(-textvariable=>\ $user)->pack();

    sub printstrings
    {
    $user .= "the name $c_name $desricpt_info\ n";
    }

    MainLoop();
    [/code]
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Why are you attempting to load the Tk module 3 times? You only need to do it once.

    Remove the -w switch and add these 2 pragmas.
    Code:
    use strict;
    use warnings;
    Fix all problems that they point out.

    Fix your indentation.

    $raw_data is a single scalar item, not a list, so why are you using a foreach loop?

    For displaying a new line each time the button is pressed, did you try the suggestion I gave you in your other question?

    Comment

    Working...