Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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


Comment on Re^4: Modifying text file
Download Code
Re^5: Modifying text file
by 2teez (Priest) 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; }
    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
Re^5: Modifying text file
by hdb (Prior) 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:

    @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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-12-29 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (188 votes), past polls