http://qs321.pair.com?node_id=264867

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

I'm new to Perl, and I am having trouble figuring out how to read a newline delimited file into my program. Here is my code so far:
use File::Find; use strict; my ($dir, @parts); open(IN, "< input.csv") or die("Couldn't open input.csv\n"); open(OUT, "> output.csv") or die("Couldn't open output.csv\n"); @parts = <IN>; #Displays total size of specified path (total includes subfolders) foreach my $dir (@parts) { print "Walking $dir\n"; # . I took this part out because it doesn't # . relate to my problem # . } print "\n\tOutput created.\n"; close(OUT);
Here is an example of the input file:

\\server\share
\\server2\share
c:\temp

I'd appreciate your help getting this thing to correctly read in the newline delimited file!

Replies are listed 'Best First'.
Re: Newline Delimited Input
by CukiMnstr (Deacon) on Jun 10, 2003 at 23:53 UTC
    well, as boo_radley already pointed out,
    @parts = <IN>;
    reads the newline delimited file into the @parts array. The only thing I can think of that might be giving you trouble is that every line in @parts has a newline at the end, but I have no way of telling if this is the problem without seeing the part you took out... does changing the above line to
    chomp ( @parts = <IN> );
    fixes your problem?

    hope this helps,

      Here is the part I took out:
      #Displays total size of specified path (total includes subfolders) foreach my $dir (@parts) { my $total; print "\nWalking $dir\n"; find(sub { $total += -s }, $dir); $total = ($total / 1024) / 1024; $total = sprintf("%0.2f", $total); print OUT "$dir, $total, mb, \n"; } print "\n\tOutput created.\n"; close(OUT);
        so you are not chomp()ing $dir... are you getting something like
        Unsuccessful stat on filename containing newline at...
        when you run your script? Did you try chomp()ing the filenames as I suggest here?

        hope this helps,

Re: Newline Delimited Input
by boo_radley (Parson) on Jun 10, 2003 at 22:34 UTC
    Well, this line reads a newline delimited file into an array
    @parts = <IN>;
    so there you go. Here's what I'm curious about, and what will probably get your real (and unspoken) question answered -- What your script (or the chunk you've posted) should do in your mind, and what it's actually doing, and how the two differ.
Re: Newline Delimited Input
by waswas-fng (Curate) on Jun 10, 2003 at 21:44 UTC
    open(IN, "< input.csv") or die("Couldn't open input.csv\n"); open(OUT, "> output.csv") or die("Couldn't open output.csv\n"); while (<IN>) { chomp; #Displays total size of specified path (total includes subfolders) print "Walking $_\n";
    #stuff you took out... Are you using File::Find?
    } print "\n\tOutput created.\n"; close(OUT);


    -Waswas