How to read every line of file into array

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

    How to read every line of file into array

    This program display a names. If the name you type match the one asked to type, the program tells you the phone number of the name. The problem is it only list the last name in the file. How to get the program to start at the first name in the file, and after you type it correct, show name number and move to the next name? All help would be graceful!!
    Code:
    open (FILE, "profile.txt") || die "Can't open File.txt: $!\n";
    @raw_data=<FILE>;
    close(FILE);
    
    # my array only loads last name in file
    foreach $_ (@raw_data)  #foreach loop to grab wanted variables
    {
     ($c_name, $descript_info)=split(/\|/, $_); #split method create pipe used escaped with \ char  two variables one for the names other for numbers
    
    } 
    sub go{
    $user= "Please type the name  $c_name \n";}
    MainLoop;
    
    sub enter{ 
    if ($E) #check user input value
    { goto contin1;} 
       
    else
    {$user= "You didn't type anything!!\n"; 
    }
    
    contin1:
    $user= "\e[H\e[J"; #clear the screen
    if ( $c_name =~ /$E/ ) #compare with user input
     {
        $user="The name -$c_name to $desricpt_info \n"; 
    my $mw_next=$main->messageBox(-message => "Remember!",-=>\&go, -type =>$user);
     } 
    else 
     {  
        $user= "The name was mistyped! Please try again.\n";
     }
    Last edited by numberwhun; Mar 25 '10, 02:11 PM. Reason: Please use CODE TAGS!!!
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    First, ALWAYS use the code tags when posting your code.

    Add these 2 pragmas and fix the problems they point out.
    Code:
    use strict;
    use warnings;
    Get rid of that goto statement. The goto statement is very rarely needed or appropriate, and is never needed/appropriate in this type of situation.

    Load your data into a hash, not an array.

    Comment

    • soundconcept
      New Member
      • Mar 2010
      • 1

      #3
      array is ok, looping is off

      I believe your file is getting into your array ok, but when you are looping through it, you are only saving the last row.

      The loop splits the row fine, but does nothing with the values until it terminates. At that point the values are set to whatever was found in the last row.

      Incidentally - you don't have to read the file into an array - you can loop across your <file> this way:
      Code:
      open FILE, "profile.txt" || die "Can't open File.txt: $!\n";
      foreach $_ (<FILE>) #foreach loop to grab wanted variables {
           #do ALL your stuff here
      }
      close FILE;
      The most important thing is, do everything you need to do inside the loop - don't read all your data in and exit the loop with only the last element in the array (the last line in your file).

      I hope this helps!
      Last edited by numberwhun; Mar 25 '10, 02:13 PM. Reason: Please use CODE TAGS!!!

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Incidentally - you don't have to read the file into an array - you can loop across your <file> this way:

        open FILE, "profile.tx t" || die "Can't open File.txt: $!\n";
        foreach $_ (<FILE>) #foreach loop to grab wanted variables {
        #do ALL your stuff here
        }
        close FILE;

        The most important thing is, do everything you need to do inside the loop - don't read all your data in and exit the loop with only the last element in the array (the last line in your file).
        Most of the time I'd agree with that, but in this case that would be the wrong approach.

        If the data is going to be queried multiple times, as it is in this case, it would be better to process the file once and store it in a data structure. The choice then becomes, what type of data structure. Since each line is a "record" of name/value pairs, storing the data in a hash becomes the most obvious and efficient data structure. Then it's a simple matter of doing a hash lookup to see if the user supplied name is in the hash.

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          And @soundconcept, please use CODE TAGS around your code, just as was suggested to the OP who started this thread, but RonB.

          Code tags are required around ALL CODE that is placed into the forums. If you do not use them, then we have to clean up behind you and put them in place for you.

          If you do not know how to use them, then maybe you should read How To Post A Question in the sites FAQ.

          Comment

          • lashaw
            New Member
            • Mar 2010
            • 8

            #6
            I should had tried to put my data set in a hash from the beginning. Hashes are good for handling large data sets.

            Thank you to everyone that responded and I will get back to you to tell you how it goes.

            Comment

            • lashaw
              New Member
              • Mar 2010
              • 8

              #7
              Sorry about the missing code tags.

              Comment

              • lashaw
                New Member
                • Mar 2010
                • 8

                #8
                I forgot to mention the array works fine in perl with is text-based. But the problem is using this coding for perl tk. Im having difficulties with this event-driven part. All help will be considered an honor.

                Comment

                • RonB
                  Recognized Expert Contributor
                  • Jun 2009
                  • 589

                  #9
                  We can't help you troubleshoot code that you haven't shown and haven't properly explained the problem.

                  Comment

                  • lashaw
                    New Member
                    • Mar 2010
                    • 8

                    #10
                    The problem is using this coding for perl tk. Im having difficulties with this event-driven part. The problem is this program is only displaying the last line in the file. Need it to display each name, and after the buttom is pressed display the next name. All help will be considered an honor.
                    [code=perl tk]

                    require Tk;
                    use Tk ':eventtypes';
                    use Tk;

                    my $mw = MainWindow->new();

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

                    $mw->Button (-text=>"names",
                    -command=>[\&printstrings, $_])
                    ->pack(-side=>"left");

                    $mw->Label(-textvariable=>\ $user)->pack();
                    sub printstrings
                    {open (FILE, "new.txt") || die "Can't open File.txt: $!\n";

                    foreach $_ (<FILE>)
                    {
                    ($c_name, $phone_info)=sp lit(/\|/, $_);
                    $user= "the word $c_name $phone_info\n";
                    }}

                    MainLoop();

                    [/code]

                    Comment

                    • RonB
                      Recognized Expert Contributor
                      • Jun 2009
                      • 589

                      #11
                      The problem is that you're over writing $user for each line in the file, which is why you end up with only the last line.

                      There are several other issues, but to fix this one problem, change this line:
                      Code:
                      $user= "the word $c_name $phone_info\n";
                      To this:
                      Code:
                      $user .= "the word $c_name $phone_info\n";

                      Comment

                      • lashaw
                        New Member
                        • Mar 2010
                        • 8

                        #12
                        Thanks RonB for the insight but how to get it to display each name after I press the button?

                        Comment

                        • RonB
                          Recognized Expert Contributor
                          • Jun 2009
                          • 589

                          #13
                          I'm not sure what you mean.

                          After making the correction I showed, it will display each/all names. Assuming that the lines are formatted as you expect and the split is successful.

                          Comment

                          • lashaw
                            New Member
                            • Mar 2010
                            • 8

                            #14
                            What I mean is how to display one name at a time when the button is pressed instead of showing the whole list all at one time.

                            Comment

                            • RonB
                              Recognized Expert Contributor
                              • Jun 2009
                              • 589

                              #15
                              First, load the data file into an array. This would be done outside of the printstrings sub.

                              The sub would track the last array index number that it accessed and update the screen with the next array element.

                              I'd use a text or listbox widgit instead of the label widgit.

                              Here's an example of each.

                              Comment

                              Working...