in reply to Removing Newline Characters from File Data (chomp not working)

Assuming that the previous suggestions of modifying the line terminator (see $/) fix your immediate issue, there are some style issues you may consider. These are some generally regarded good practices which you are free to ignore at your discretion:

  1. Starting each script and module with use strict;use warnings; can save you a bunch of headaches. The virtues of these pragmas have been extolled extensively on this site. For example, Use strict warnings and diagnostics or die.
  2. You should consider using 3-argument open instead of 2. It allows for some more security against malicious people and messing stuff up. If your case, line 5 would be changed to

    open(plate_file,'<',$filename.".csv") or die "Can't open: $!";

    See open,perlopentut for some more details.

  3. Rather than building your own paths, you might consider using File::Spec to do it for you, in a platform independent way.
  4. In fact, rather than rolling your own CSV parser, consider using Text::CSV.
  5. Regarding your second question, in list context a regular expression returns the captured expressions. So you could write something like:

    #!/usr/bin/perl use strict; use warnings; my @data; while(<DATA>) { my @row = split /,/,(/(.*)\n?/)[0]; # <-- nice and clear, right? shift(@row); push(@data,@row); } print join "\n", @data; __DATA__ 1,2,3,4 2,3,4,5 3,4,5,6

I could go on, but that should give you plenty to chew on.

Replies are listed 'Best First'.
Re^2: Removing Newline Characters from File Data (chomp not working)
by mlux (Initiate) on Aug 27, 2009 at 16:56 UTC
    Thanks, these suggestions are helpful. Regarding #5, that makes sense now - I had thought that would simply store true or false in the array. I do have a couple questions about the expression you used in #5. I get that /(.*)\n?/ matches all characters before the newline character, but I don't understand the outer outer parenthesis or the [0]. Also, is the ? necessary? Each line should end /n so is the ? there because of good practice or for some other reason?
      The purpose of the ? on the newline is to make it not essential that the line ends with a new line. This handles the edge case where the file is not ended with a newline character - otherwise, that wouldn't match the expression and it would be silently dropped. For some reference on regex construction, see perlre and perlretut.

      The purpose of the parentheses is to put the regular expression in list context, and then the [0] index takes the first and only element, which is the expression in the parentheses. Without that, the regular expression would be in scalar context because of the split function, and would return only the true/false. Perl's ability to modify functional behavior's based upon context is one of its great strengths, though it can also cause significant confusion to new acolytes.

        New acolytes like me, hah. Thanks, your explanations have really helped.