Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

replace middle spaces and write to another file

by sufi (Initiate)
on Feb 02, 2011 at 04:33 UTC ( [id://885662]=perlquestion: print w/replies, xml ) Need Help??

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

I want to replace spaces with comma in a text file and then want to write this newly edited data to other text file. I wrote this code : open (FD, "2testload.txt"); @aln=<FD>; close FD; foreach $aln (@aln) { $aln =~ s/\s/,/g; } open (FD, ">2outfile.txt"); print FD @aln; close FD It is working well but it writes all the content of first file (all lines) to second file in a single line instead of writing each line to separate lines of new file result is 0,plane,woodworking,2006-09-04,15:36:28,0,kanna,plane,2006-09-04,15:36:28 but it should be like this 0,plane,woodworking,2006-09-04,15:36:28 0,kanna,plane,2006-09-04,15:36:28
  • Comment on replace middle spaces and write to another file

Replies are listed 'Best First'.
Re: replace middle spaces with commas
by Anonyrnous Monk (Hermit) on Feb 02, 2011 at 04:56 UTC
    I want commas in middle spaces not at the end, but here I got commas at the end also...

    AFAICT, your problem arises from using \s in the substitution s/\s/,/g, instead of a plain space character, i.e. s/ /,/g.

    \s stands for "whitespace", which includes newline (\n), so the newlines are turned into commas, too.

Re: replace middle spaces with commas
by CountZero (Bishop) on Feb 02, 2011 at 06:48 UTC
    And please put <code> ... </code> tags around your code.

    Update: If you do not mind the original file is changed "in place" you can do this:

    use Modern::Perl; use Tie::File; my @tied_array; tie @tied_array, 'Tie::File', 'test.txt', autochomp => 1; s/\s+/,/g for @tied_array;
    Which will change
    dit is een test hier is een andere zin en tenslotte de finale lijn
    into
    dit,is,een,test hier,is,een,andere,zin en,tenslotte,de,finale,lijn

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: replace middle spaces with commas
by ikegami (Patriarch) on Feb 02, 2011 at 06:35 UTC

    Each line ends with a newline, and newline is a form of whitespace (/\s/). Either chomp off the line endings and add them back when you output, or be more selective in what you replace (i.e. match / /, /\t/ or /[ \t]/ instead of /\s/).

    Update: Sorry, this is effectively a duplicate of the earlier post which I somehow missed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://885662]
Approved by Anonyrnous Monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2025-03-19 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (59 votes). Check out past polls.