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

Replies are listed 'Best First'.
Re: Replace spaces with commas and write to other file
by Corion (Patriarch) 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 (Monsignor) 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
    A reply falls below the community's threshold of quality. You may see it by logging in.