473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. tie my @file_lines, 'Tie::File', $File2 or die;
  2. open(MYINPUTFILE, '<' ,"$File1");
  3. foreach my $line (<MYINPUTFILE>) {
  4. chomp ($line);
  5. @file_lines = grep /^"$line"/, @file_lines;
  6. untie @file_lines or die "$!";
  7. }
  8.  
Feb 21 '13 #1
6 1607
RonB
589 Expert Mod 512MB
Can you explain what you intended this line to do?
Expand|Select|Wrap|Line Numbers
  1. @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?
Expand|Select|Wrap|Line Numbers
  1. @file_lines = grep /^"Yellow Red Blue"/, @file_lines;
Feb 22 '13 #2
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.
Feb 22 '13 #3
RonB
589 Expert Mod 512MB
It's looking/grepping for the full string, not each word individually.
Feb 22 '13 #4
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
Feb 22 '13 #5
RonB
589 Expert Mod 512MB
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.
Feb 22 '13 #6
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.
Feb 22 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Aaron Collins | last post by:
Hello, i have a string that was entered in a web form and stored in a mysql database where the user entered carriage returns as well as <br>'s after each line in the form. the data is stored as...
2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
2
by: Bruce Russell | last post by:
This may sound stupid but I can't rename the WebForm1.aspx in the solution explorer. The file is located in my local web server at C:\Inetpub\wwwroot\Lab3-VB-Starter\WebForm1.aspx Is there...
1
by: Benny Raymond | last post by:
I have lines of data coming from a server being written to a RichTextBox... The problem is that if I let this program run for a long period of time there are hundreds of lines of text showing up in...
5
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
1
by: derik | last post by:
I hav a text file with N number of lines . i am able to read the file till the end .. in between many lines are starting with #. these lines starting with # are to be omitted. pleast help me in...
2
by: pandurusankar | last post by:
Hi, Need a pointer for managing File type button in HTML page, i am able to handle all the components in HTML using IEAutomation module like: text box, links, radio button, list box etc. but i...
0
by: aadsaca | last post by:
Deleting/Removing lines on Text File in VB -------------------------------------------------------------------------------- Hi there, i just want to know the syntax on how to remove line on...
6
by: Damien87 | last post by:
Hi, I have a .data file which is essentially just a text file with a bunch of numbers. It's in the format "xx yy zzz abc ccf fffd sss xxx xx sss qq" Basically it is a random amount of numbers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.