Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Replace spaces with commas and write to other file

by sufi (Initiate)
on Feb 14, 2011 at 08:38 UTC ( #887940=perlquestion: print w/ replies, xml ) Need Help??
sufi has asked for the wisdom of the Perl Monks concerning the following question:

Greetings everyone I have data in a text file in which each line of the file has spaces among words, I want to replace all of these spaces with Commas and then write this new content to another text file with each line on new line of 2nd text file. I want to know: 1. Open file 2. Read it in array for accessing each line individually 3. Replace spaces with commas in each line 4. Write these new comma separated lines to new text file My data format: 0 boomerang 7 1 2005-12-15 19:31:50 0 shop 7 1 2005-12-15 19:31:50 0 bibtex 8 1 2005-12-13 08:42:37

Comment on Replace spaces with commas and write to other file
Re: Replace spaces with commas and write to other file
by Corion (Pope) on Feb 14, 2011 at 08:47 UTC

    See replace middle spaces and write to another file and its replies. Where exactly do you have problems? You have the algorithm and the general code layout already. All that remains is to produce the code for each step, which is not hard if you have learned Perl.

    If this is a homework assignment or a job requirement, please understand that we will not do your work for you, the same as we don't collect your payment for you. While we will gladly assist you in your way of learning Perl, we expect you to do some work of your own.

Re: Replace spaces with commas and write to other file
by Ratazong (Prior) on Feb 14, 2011 at 08:49 UTC

    Hi

    Your algorithm looks fine - however you can optimize it by not reading the whole file into an array, but by reading line-by-line, doing the blank-to-comma-exchange on the current line, and then writing it to the output-file.

    Regarding your "I want to know"-line I am puzzled: where exactly is your problem? In Text file opening problem you already asked on how to open a file. And in replace middle spaces and write to another file you asked on how to replace spaces by commas. Maybe you can illustrate your issues with code you have already written.

    Rata
      HI Rata I have data in a text file in following format: 0 boomerang 7 1 2005-12-15 19:31:50 0 shop 7 1 2005-12-15 19:31:50 0 bibtex 8 1 2005-12-13 08:42:37 I want to replace all of these spaces with Commas and then write this new content to another text file.But when I run the following code: open (FD, "smallrelation.txt"); @aln=<FD>; close FD; foreach $aln (@aln) { $aln =~ s/\s/,/g; } open (FD, ">2outfile.txt"); print FD @aln; close FD It gives me following out put: 0,plane,woodworking,2006-09-04,15:36:28,0,kanna,plane,2006-09-04,15:36:28 Now the spaces have been replaced with commas but all these lines have been shown on one line of output file. I want: Each of these lines should be appear on separate lines of output file. like 0,plane,woodworking,2006-09-04,15:36:28 0,kanna,plane,2006-09-04,15:36:28 Thank you

        So, maybe you want to output a line separator after you output each line?

        Or maybe you want to only replace the space character and not all whitespace (including line separator characters)?

        Hi

        It would be great if you could use <code>-tags around your code and data input/output ... so I'm just guessing. It seems your issue is that only one, long line is shown in the output-file.

        That's easy to solve: if you print \n a linefeed is produced. So you should add this linefeed after each element. So after $aln =~ s/\s/,/g; you could add $aln .= "\n";. Of course there are many other ways to reach the same effect, e.g. by using the special variable $,.

        HTH, Rata

        sufi:

        You just about have it. You're replacing all whitespace with commas. The problem is that the end of line \n is a whitespace character. You're turning it into a comma. You should chop off the end of line before you do your replacement, and then you can add it back when you print the line. Then you'll have solved your problem.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-12-20 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls