Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

putting commas separated values into a new file line by line

by Anonymous Monk
on Aug 11, 2020 at 16:08 UTC ( [id://11120614]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

from command line, we are passing multiple values separated by commas such as sydney,delhi,NY,Russia. these values are getting stored under $runTest in the script. now i want to create a new file with contents of $runTest but as line by line. example

INPUT (contents of $runTest): sydney,delhi,NY,Russia OUTPUT (under new file: myfile): sydney delhi NY Russia

Replies are listed 'Best First'.
Re: putting commas separated values into a new file line by line
by choroba (Cardinal) on Aug 11, 2020 at 16:15 UTC
    To create a new file, use open.
    open my $out, '>', 'new_file' or die $!;

    If you just need to output the string, you can replace commas by newlines:

    my $runTest = 'sydney,delhi,NY,Russia'; print {$out} $runTest =~ tr/,/\n/r;

    Use say instead of print if you want a newline after Russia.

    If you want to store the lines in an array, use split:

    my @lines = split /,/, $runTest;

    BTW, Sydney, Delhi, and NY are cities, while Russia is a country.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      i don't understood completely, i just want to have those command line passed values that are further stored under $runTest in a new file as line by line

      got below error while running the script

      Bareword found where operator expected at ./fileTest.pl line 10, near "tr/,/\n/r"

      syntax error at ./fileTest.pl line 10, near "tr/,/\n/r"

        What Perl version are you using?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11120614]
Approved by choroba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-24 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found