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

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

Replies are listed 'Best First'.
Re^4: Modifying text file
by torres09 (Acolyte) on Jun 05, 2013 at 13:04 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;

    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

Re^4: Modifying text file
by torres09 (Acolyte) on Jun 06, 2013 at 09:42 UTC

    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 :)