Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Modifying text file

by torres09 (Acolyte)
on Jun 05, 2013 at 12:57 UTC ( #1037199=note: print w/ replies, xml ) Need Help??


in reply to Re: Modifying text file
in thread Modifying text file

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>


Comment on Re^2: Modifying text file
Re^3: Modifying text file
by toolic (Chancellor) on Jun 05, 2013 at 12:59 UTC
    Show the code that you have so far. Then we can make specific recommendations.
      #!/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;

      so this is the code i was hoping if in @names12 we can have elements of @myNames ending with ',' . I am new to perl so I really appreciate you helping out

        Since you already have all your file words in an array, then you can do like so:

        print join ',' => @names12;
        This would print out all your data follow with a comma. See: the use of join

        However, I would not do it this way, since you are going through the file, line by line, there is no need to store that the words after slitting.
        You can simply split and print at once like so: (Using your code.)
        while(<MYFILE>){ chomp; print join ',' => split; }
        Update:
        Please, use warnings and strict in your code.
        Also use autodie or check the return status of function open.
        Using a three argument of open function is also encouraged.

        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

        As you are new to Perl I want to comment at least on one line in your code:

        @names12[$i]=@myNames[$i]+',';

        • When you want to access one element in an array @names12, the correct syntax is $names[$i] as any individual element is a scalar. So you have to change @ to $.
        • To concatenate two strings, you use the . operator, like $string1 . $string2

      hey if in your code I wish to write this in a new text file , how should I proceed and

      secondly if i wish to put it in word by word in an array with separation by a comma , how should I proceed

        hey if in your code I wish to write this in a new text file , how should I proceed and

        Proceed to perlintro and read it :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-09-16 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls