Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delimiting Text file for reading

Status
Not open for further replies.

atsea

Technical User
Feb 27, 2005
51
JP
Currently I have a simple program that takes user input and checks it against a list of values in a .txt file.

when using windows it works fine (all records are delimited by a carriage return, I used the "chomp" function for this)

but, when I moved the .txt file to a linux platform the records dont seem to be delimited anymore.

Here is some of my sample data (id.txt).

11000101
11000109
11000112
11000115
11000127
11000132
11000138
11000139
11000212
11000215
11000216

here is a piece of my code:

############################################################
#opens the file and puts the contents into an array
open (DAT, "id.txt") or die("Could not open file!");
my@loginData = <DAT>;
close (DAT);


#checks the value entered by the user against the values in the array
foreach my$login (@loginData)
{
#ensures that the usernames are seperated in the txt file
#carriage return is the delimeter
chomp($login);


if (lc($login) eq lc($loginName))
{
print "Match"
}
}

print "No Match Found";
############################################################

using windows it will find a match...on linux it wont find anything (however if I make a file with a couple of test records in linux and physically enter the return at the end of the line it seems to work fine). Is there some special way to save the id.txt file to retain the properties it has in windows? Do I need to create a regular expression to detect the carriage return in linux?

Im sure the answer is simple, but my knowledge of linux is limited.
 
Linux uses a different sequence to specify an end of line and carriage return. Thats why when you enter the return in linux it works.
I would first test if it is reading in a line per element in @loginData.
In the foreach loop after the chomp, put something like this:
print "$login\n";

If it prints out the entire file on one line, then you will have to run a win->linux file converter over it...
Or you could use a simple substitute match to replace the new line char with a standard linux cr.
Its been a while since ive played with linux so I cant remember the native cr.
Some versions of linux comes with a neat command line utility called 'win2linux' or 'win2unix' which will fix the control chars in a text file.

If it prints a line per line of the file, then its obviously the 'eq' thats causing the problem.
 
Excellent! that was exactly the information I was looking for.

I ran a little perl script on my windows .txt file that changes the windows <CR><LF> to linux <LF> (the linux cr).

All is well...for now...

Thanks majorbiff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top