Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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

Replies are listed 'Best First'.
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?
[Corion]: Yay, vacation time now - one week off work (but a Perl workshop in the middle ;)) )
[talexb]: TStanley I just realized that I was in your area for a convention back in April. We were at the high school for the weekend, the one with the long driveway. Nice spot.
[TStanley]: I am heading to Arlington, Virginia next week, then I get to spend an entire week at home. Looking forward to that as I will have spent 7 weeks on the road by the end of next week
[talexb]: That's a long road trip. I haven't travelled for business in years.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-06-23 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (548 votes). Check out past polls.