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

Re^4: Modifying text file

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

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

#!/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

Replies are listed 'Best First'.
Re^5: Modifying text file
by 2teez (Vicar) on Jun 05, 2013 at 13:19 UTC

    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; }
    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
Re^5: Modifying text file
by hdb (Monsignor) on Jun 05, 2013 at 13:33 UTC

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


    • 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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1037201]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-02-23 12:58 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (302 votes). Check out past polls.