Help with removing lines in a file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • starlight849
    New Member
    • Jun 2009
    • 82

    Help with removing lines in a file.

    I have two files. File1 contains a list of items that I want to remove if found in file 2.

    File1: Yellow Red Blue
    File2: Orange Pink Purple Blue

    I would like the results of file 2 to be Orange Pink Purple as Blue is in the first file, it should be removed.

    Could anyone give some advice as why the logic in my code is not working?

    Code:
    tie my @file_lines, 'Tie::File', $File2 or die;
    open(MYINPUTFILE, '<' ,"$File1");
    foreach my $line (<MYINPUTFILE>) {
    chomp ($line);
    @file_lines = grep /^"$line"/, @file_lines;
    untie @file_lines or die "$!";
    }
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Can you explain what you intended this line to do?
    Code:
    @file_lines = grep /^"$line"/, @file_lines;
    Do you understand what that is actually doing vs what you intended?

    Would the problem be more clear if we replace $line with your sample data?
    Code:
    @file_lines = grep /^"Yellow Red Blue"/, @file_lines;

    Comment

    • starlight849
      New Member
      • Jun 2009
      • 82

      #3
      It looks like it's not catching the differences and storing them in an array.
      I'll work on it for a little while and see what I can come up with. Thanks for the tip.

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        It's looking/grepping for the full string, not each word individually.

        Comment

        • starlight849
          New Member
          • Jun 2009
          • 82

          #5
          But I'm iterating through each line in the file. and assigning them one at a time to $Line. So on one iteration the var is Yellow then next is Orange then next is Blue.
          Sorry for the confusion. File one is sorted like this:
          Yellow
          Red
          Blue


          Then file 2 is sorted like this:
          Orange
          Pink
          Purple
          Blue

          ..since Blue existed in the first file it should be removed and file 2 should reflect.
          Orange
          Pink
          Purple

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            There are modules available that can accomplish, but I suspect that this is a homework assignment and your instructor wants you to figure out the logic.

            As a hint, I'd load file 2 into a hash instead of an array provided that maintaining the order is not a requirement.

            Comment

            • starlight849
              New Member
              • Jun 2009
              • 82

              #7
              Oh, this isn't a homework assignment. I was trying to learn to use file::tie. I was just work this as an example to try and learn the syntax. I'll try the hash.

              Comment

              Working...