Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Modifying text file

by torres09 (Acolyte)
on Jun 05, 2013 at 12:43 UTC ( #1037197=perlquestion: print w/replies, xml ) Need Help??
torres09 has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I have a text file , so I want to convert that text file such that each word has a comma following it. please help

Replies are listed 'Best First'.
Re: Modifying text file
by toolic (Bishop) on Jun 05, 2013 at 12:52 UTC
    Here is one way to get started using s///
    use warnings; use strict; while (<DATA>) { s/(\w+)/$1,/g; print; } __DATA__ This is a word. Here is another sentence.

    Prints:

    This, is, a, word,. Here, is, another, sentence,.

    See also perlintro

      hey I have extracted my text file into an array by splitting it , so can I put it in a new array with each word terminated by comma , If we can plz tell me <\p>

        Show the code that you have so far. Then we can make specific recommendations.

      the problem I am having is say I have entry as -9.10 it will split it as -9,10 so it breaks one entity into two , but I want it to be a ......,-9.10,..... . so can you please help out

Re: Modifying text file
by hdb (Prior) on Jun 05, 2013 at 13:12 UTC

    I am smelling an XY Problem. So here is some code that takes $text and creates an array @words containing all the words in $text. In addition, it adds a comma to each word, but my guess is, that you are not really interested in that...

    use strict; use warnings; my $text = <<EOTEXT; This is a word. Here is another sentence. EOTEXT my @words = $text =~ /\b(\w+)\b/g; print "@words\n"; $_.="," for @words; print "@words\n";

    Thanks to toolic for the sample text.

      Sir

      I actually want it , i.e. I want that each word in the text file should be in an array , with a comma in between .Even the spaces

        Apologies if you perceived my answer as being patronizing.

        If you want to split your text into words AND keep the spaces, split might be an alternative. Using a capture group in the regex in split, the delimiters are not discarded. So splitting on words will give you the words and the characters inbetween:

        use strict; use warnings; use Data::Dumper; my $text = <<EOTEXT; This is a word. Here is another sentence. EOTEXT my @words = split /\b(\w+)\b/, $text; print Dumper \@words;

        results in

        $VAR1 = [ '', 'This', ' ', 'is', ' ', 'a', ' ', 'word', '. ', 'Here', ' ', 'is', ' ', 'another', ' ', 'sentence', '. ' ];
Re: Modifying text file
by 2teez (Priest) on Jun 05, 2013 at 13:07 UTC

    Hi torres09,
    Welcome to the Monastery.
    Though you have been given a head up, I think it worth the note to check this How do I post a question effectively?.
    And in addition to what has been given, you could also think of use the module Text::CSV or Text::CSV_XS

    Update: Code to illustrate...

    use warnings; use strict; use Text::CSV_XS; my $csv = Text::CSV_XS->new( { binary => 1, auto_diag => 1 } ); while (<DATA>) { for ( [split] ) { $csv->print( \*STDOUT, $_ ); print $/; } } __DATA__ Mary had a little lamb whose fleece was white as snow.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Modifying text file - I like PIE :-)
by space_monk (Chaplain) on Jun 05, 2013 at 13:23 UTC
    Something like:
    perl -pie 's/(\w+)/$1,/g' yourFile
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037197]
Approved by toolic
help
Chatterbox?
[haukex]: Corion: Yes exactly, in the author tests I don't worry about portability as much, I also don't list the author tests' dependencies in Makefile.PL
[haukex]: I figure someone who wants to contribute will know how to install the missing modules ;-) Not the nicest way to go but I don't think many people are using my modules yet
[ambrus]: Corion: some of these stupid syntax highlighters assume that too. just look at the table in http://perldoc. perl.org/ functions/pack. html for example.
[haukex]: ..."yet" ;-) I haven't had to deal with Dist::Zilla yet but I've heard about how it's a big setup
[ambrus]: I really don't like automagic stuff. I'm happy when computers do exactly what I tell them, even if that means they sometimes do the wrong thing.
[ambrus]: And I don't much like syntax highlighters. If you need a syntax highlighter to understand your code, then your code is written unclear.

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2017-02-27 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (385 votes). Check out past polls.