On Thursday, August 14, 2003, at 01:11 PM, Trina Espinoza wrote:

Let me start of with a smaller question. . ..

I saw your early question, but it is kind of vast. :) How far you need to go depends on how many types of files you need to support. You could be talking about using a lot of modules and writing quite a bit of code and I bet that wasn't the answer you were looking for.


Data Munging with Perl is an excellent book on this topic, if you're interested.

How do you get rid of returns?

This question I can handle!


I known chomp gets rid of new lines, but I don't know how to get rid of returns.
My code accepting data from the user. I just need to start by filtering out \n's and
\r's

What chomp() gets rid of, is actually platform dependent. On UNIX systems you are right, it's line feeds. On Windows, it's carriage return line feed pairs. On Macs prior to OS X it was carriage returns. \r and \n change meanings on these platforms similarly.


I tried the code (snippet below), but there seems to be 2 problems:
1) my code ignored the if statement

I doubt that, see below.


2)If my $line did have a return, I wouldn't know now to remove it.
Could I say something like

if (/\r/) {
    $line = grep(/[^\r]/,$line)

No, grep() is for searching lists. Here's an easy way to remove carriage returns and line feeds on any system:


$data =~ tr/\012\015//d;

EXISTING SNIPPET OF CODE
-------------------------
open(SOURCE, $source_file) || die "Cannot open \"$source_file\: $!";
my $line;
while ($line = <SOURCE>) {
    if(/\n) {

A bare match, like you have in this if statement matches against $_, Perl's default variable. You didn't put the line in $_ though, you put it in $line. Don't do that. Let's let Perl do the work for us. Drop $line altogether and just use:


while (<SOURCE>) {
        tr/\012\015//d;

That assigns to $_. The second line removes all carriage returns and line feeds. You can drop you whole if clause then.

Hope that helps. Good luck.

James

        chomp;
    if (/\r/) {
        print "This line has a \\r\n";

 }
}


Thanks! -T


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to