Solved

Getting rid of \r ?

Posted on 2008-06-12
5
176 Views
Last Modified: 2010-03-05
Hi,

I made a csv file on a windows machine, its contents are just like:

  5 10 15
  2 4 6
  3 6 9

I tokenize the input, then just print the matrix as a test. I print brackets around the numbers. It works fine on a windows machine. It looks like:

   Row 0: [5] [10] [15]
   Row 1: [2] [4] [6]
   Row 2: [3] [6] [9]

When I move to a solaris machine running perl 5.8.8, for some reason, the closing bracket that was supposed to be at the end of the line eats what was supposed to be the first character in the line! The last printed line looks ok:

   ]ow 0: [5] [10] [15
   ]ow 1: [2] [4] [6
   Row 2: [3] [6] [9]

My print statements are just like:

    print("Row", $r, ": ");
    # for all columns
       print("[");
       print($token);
       print("]");
    #end all columns
    print("\n");

It's definitely something with the value of $token. If I remove that print statement, everything gets printed  fine. I'm thinking that since I made the csv file on a windows machine, it's got some windows character for a new line that is making the output get messed up on the solaris machine?

Is there a way to completely trim any junk off the end of the token to see if that's a problem?

Thanks

   
0
Comment
Question by:DJ_AM_Juicebox
5 Comments
 
LVL 9

Expert Comment

by:Suhas .
ID: 21770163
to end the columns
try print("\r\n");
0
 
LVL 9

Accepted Solution

by:
Rob Siklos earned 500 total points
ID: 21770394
try this before you print $token:

$token =~ s/\s+$//;
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21771177
@rob263

Yeah that works pefectly. But is there anyway to assign the token as the result of the regex operation? Something like:


$token = "155\r\n";

$token = $token =~ s/\s+$//;

# token is now just "155".


Thanks
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21771212
nevermind, i need to get some sleep.

Thanks
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21771520
The most common method is to chomp the line when you read the file.  
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question