Separating Text Fields by comma

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jw01
    New Member
    • Dec 2006
    • 19

    Separating Text Fields by comma

    Hi,
    I have created a text widget with multiple text fields using Perl Tk. So its like a web form e.g

    Name: (user entered xyz)
    Address:(user entered 123)
    Age:(user entered 77)

    When a user enters text in the fields and submits it, the data gets copied to a csv file. So the csv file looks like this:

    NamexyzAddress1 23Age77

    I want the csv file to look like:

    Namexyz,Address 123,Age77

    How do I get the comma to be added to the text entered by the user

    Thanks
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Where is your code that writes to the file? We can't tell you how to change your code without first seeing what you have.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by miller
      Where is your code that writes to the file? We can't tell you how to change your code without first seeing what you have.

      monkies maybe? :)

      Comment

      • jw01
        New Member
        • Dec 2006
        • 19

        #4
        Here is the code:

        use Tk;

        open D, ">form3.csv ";

        $mw = MainWindow->new;
        $mw->title("CR FORM");
        $f = $mw->Frame->pack(-side => 'bottom');
        $f->Button(-text => "Done",-command => sub {exit})->pack(-side=> 'left', -anchor => 'ne');
        $f->Button(-text =>"Submit",-command => sub{print D %info})->pack(-side => 'bottom',-anchor => 'nw');

        $t = $mw->Scrolled("Text ",-width => 40,-wrap => 'none')->pack(-expand => 1, -fill => 'both');
        foreach (qw/Name Address Age/) {
        $w = $t->Label(-text => "$_:", -relief => 'groove', -width => 100);
        $t->windowCreate(' end', -window=> $w);
        $w = $t->Entry(-width => 100, -textvariable => \$info{$_})->pack(-side => 'left', -anchor => 'w');
        #@info = split(',', $info, 1);
        $t->windowCreate(' end', -window => $w);
        $t->insert('end' , "\n");

        }
        $t->configure(-state => 'disabled');

        MainLoop;

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Tk is almost like learning a whole new scripting language! Where in that code is the data written to a file?

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Oh, I see now:

            Code:
            -command => sub{print D %info})
            Maybe like this:

            Code:
            -command => sub{print D join(',' %info)})

            Comment

            • jw01
              New Member
              • Dec 2006
              • 19

              #7
              Well I had tried this earlier:

              -command => sub{print D join(',', %info)}

              But all it does is separate the enteries in the excel sheet into different cells..It doesnt add a comma.

              I want it to be displayed like this:
              Name xyz, Address 123, Age gds

              where xyz,123 and gds are the enteries in the widget form.

              Thanks

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                if you want a comma in a cell I believe you have to quote the string, try this:

                Code:
                use Tk;
                
                open D, ">form3.csv";
                [B]my $cell = qq~"Name $info('Name'},Address $info{'Address'},Age $info{'Age'}"~; [/B]
                $mw = MainWindow->new;
                $mw->title("CR FORM");
                $f = $mw->Frame->pack(-side => 'bottom');
                $f->Button(-text => "Done",-command => sub {exit})->pack(-side=> 'left', -anchor => 'ne');
                $f->Button(-text =>"Submit",-command => sub{print D [B]$cell[/B]})->pack(-side => 'bottom',-anchor => 'nw');

                Comment

                • jw01
                  New Member
                  • Dec 2006
                  • 19

                  #9
                  Yes now it adds the comma after each label..But its not reading the input entered by the person in the textbox:

                  $w = $t->Entry(-width => 100, -textvariable => \$info{$_})->pack(-side => 'left', -anchor => 'w');

                  Should the $info{$_} be changed?? I have been playing around with this entry and the $cell..but I cannot get it to read the text entry and add the comma after it.

                  Thanks

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Sorry, I have no idea. If you don't get an answer here try asking on the www.Tek-Tips.com perl forum.

                    Comment

                    • jw01
                      New Member
                      • Dec 2006
                      • 19

                      #11
                      previous line:

                      w = $t->Entry(-width => 100, -textvariable => \$info{$_})->pack(-side => 'left', -anchor => 'w');

                      Change made

                      w = $t->Entry(-width => 100, -textvariable => \$info{"," , $_})->pack(-side => 'left', -anchor => 'w');

                      It now enters a "comma" after writing every input to the csv..But when I open the csv, it says file type for recognized and hence adds an extra "empty box" like element to the string..

                      How do i get rid of that??

                      Thanks

                      Comment

                      • KevinADC
                        Recognized Expert Specialist
                        • Jan 2007
                        • 4092

                        #12
                        this just looks wrong:

                        Code:
                        \$info{"," , $_}

                        I'm surprised that doesn't return an error or warning.

                        Comment

                        Working...