Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

text to array

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

Hey

I have a text file with large amount of data , and I wish to split it such that each word occupies one array element and after every word there should be a comma . So if anyone can help I will be grateful

Comment on text to array
Re: text to array
by Corion (Pope) on Jun 06, 2013 at 07:43 UTC
      #!/usr/local/bin/perl open (MYFILE, 'test1234.txt'); while (<MYFILE>) { chomp; @myNames= split(/\t/, $_); $i=0; print "@myNames \n"; while($i<=$#myNames) { $names12[$i]=@myNames[$i].','; $i++; } } print "@names12 \n"; exit;

      this is what i did , so if you can point out the mistakes , it will help

        Your mistake is that you do not tell us what you expect, and how your program fails to work for you.

        Also, please show 10 lines of relevant input, and the corresponding output, together with what the output should be.

      #!/usr/local/bin/perl open (MYFILE, 'test1234.txt'); while (<MYFILE>) { chomp; @myNames= split(/\t/, $_); $i=0; print "@myNames \n"; while($i<=$#myNames) { $names12[$i]=@myNames[$i].','; $i++; } } print "@names12 \n"; exit;

      this is what i did , so if you can point out the mistakes , it will help

        $names12[$i]=@myNames[$i].','; #should be $names12[$i]=$myNames[$i].',';

        Actually there's lots more issues in your program than this one. Its really a case of if you want to solve this properly, don't start from where you are at present! :-)

        Why are you splitting on tabs when you indicated you wanted to split out words before? The answers given in your previous question do things much more efficiently than your code, so what is wrong with them?

        Please give an example of the text you want to split and what you want it to look like at the end

        Something roughly like this will put all your data in @names12:

        # execute as perl -n program.pl < myTextFile #!/usr/bin/perl # get the words from current text block and push into array.... while (/(\w+)/g) { push @names12, $1; } END { # your array contains words, now add comma to printout print join(',', @names12); }
        If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
        If your words are tab separated, the following should work
        #!/usr/local/bin/perl use strict; use warnings; open (my $wordfile, '<', 'test1234.txt')|| die "Could not open test123 +4.txt, $!"; while (<$wordfile>){ print join ',', split(/\t/, $_); exit; }
        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: text to array
by space_monk (Chaplain) on Jun 06, 2013 at 07:44 UTC
    This is almost a duplicate of an earlier question (Modifying text file). There are plenty of answers there. You can respond or amend the question without creating a new one.
    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://1037390]
Approved by Jim
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-12-26 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (174 votes), past polls