Karger78 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, For some reason i am having issues with the split function. Here is what I am trying to do. Load a text file into an array and split on new line. Seems simple, but i am having issues. Any help wouldbe appreciated.
#!/usr/bin/perl open FILE, $ARGV[0] or die "Can't open $ARGV[0] for reading: $!\n"; @file = <FILE>; close FILE; @Loglines = split(/\n/,$file); foreach (@Loglines){ print "Line: $_\n"; }

Replies are listed 'Best First'.
Re: Split on new line
by gone2015 (Deacon) on Feb 21, 2009 at 16:24 UTC

    If you'd used use strict, it might have brought to your attention the fact that you are attempting to split a variable, $file, which you haven't declared.

    If you'd used use warnings, it would have told you that $file is undefined when you attempt to split it.

    In any case, @file = <FILE> has already read the file into an array of lines. So no split is required and you can simply: foreach (@file) Note, however that the lines in the array have the newline at the end, so you may want to either chomp the individual lines, or chomp the entire array.

    Mind you, it's possibly simpler to:

    open FILE, $ARGV[0] or die "Can't open $ARGV[0] for reading: $!\n"; while (<FILE>){ print "Line: $_"; }
    ...though you may want the whole thing in an array for other reasons.

Re: Split on new line
by toolic (Bishop) on Feb 21, 2009 at 16:27 UTC
    One issue can be resolved by adding use strict; use warnings; to your code. You will get messages to the effect that $file is not defined (amongst other things).

    When you slurp all lines of your file into @file, each element of the array should contain one line ending in a newline character. You can prove this by printing the contents of the array as follows:

    use Data::Dumper; print Dumper(\@file);

    If you then want to remove the newline characters, use chomp:

    chomp @file;
Re: Split on new line
by McDarren (Abbot) on Feb 22, 2009 at 08:16 UTC
    You have an answer to your immediate problem, but there is something else that you may wish to consider.

    Depending on the size of your log file, and the specs of your machine, reading the entire log file into an array may not be such a good idea. This is because memory must be allocated for the entire file. And if you happen to be dealing with a multi-gigabyte file, this may become a problem.

    Generally, a better approach is to read the file line by line, and process each line as you go. For this, a while loop can be used. Example:

    #!/usr/bin/perl use strict; use warnings; # 3 argument form of open using a lexical variable is considered bette +r practice # see perldoc -f open open my $file, '<', $ARGV[0] or die "Can't open $ARGV[0] for reading: +$!\n"; while (my $line = <$file>) { print $line; chomp($line); #if you need to # any other processing.... }
    Using the above method, only one line of the file is read into memory at a time, which is much more memory-efficient.

    Hope this helps,
    Darren :)