Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Perl (http://www.programmingforums.org/forum21.html)
-   -   Remving new lines from a file (http://www.programmingforums.org/showthread.php?t=2389)

eggsy Feb 21st, 2005 4:28 PM

Remving new lines from a file [Resolved]
 
Hi all suppose I had the following flat text file

lol;laugh out loud (NEWLINE)
m8;mate (NEWLINE)
l8r;later (NEWLINE)

etc

I want to try and put them into an array

EG. @array = split(/;/, <FILE>);

but it only works for the first line, stumbling at the new line character?

Does anyone know if theres a way to strip out all the new line characters from the file???

Sorry if this is a real basic question, Perl newbie here

Thank you all who read this

Eggsy

Mad_guy Feb 21st, 2005 4:59 PM

Use the following:

:

#!/perl/bin
my @words;
open(FILE, "<file.txt") or die("Can't open file.");
while($currentline = <FILE>) {
 chomp($currentline);
 push(@words,$currentline);
}
close(FILE);
foreach $word (@words) {
 print $word." ";
}
print "\n\nDone. Hit RETURN to exit...";
$exit = <STDIN>;

Taa-daa.

mackenga Mar 24th, 2005 9:20 AM

You could slurp and split the file, too; to get a list of the lines in a file, you could do:

:

local $/;
my @lines = split("\n", <IN>);


That 'local $/;' line makes $/, the input record separator, locally undefined so you can 'slurp' the whole file rather than a line at a time.


All times are GMT -5. The time now is 6:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC