Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Reading File Into Array

by Irishboy24 (Sexton)
on Mar 28, 2016 at 01:44 UTC ( [id://1158919]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, Noob here. please advise.I'm trying to read a File into an array and print each element of the array on a newline.
cat Arsenal Cech Bellerin,Per,Koscielny,Nacho Ramsey,Coquelin Alexis,Ozil,Welbeck Giroud
Here is what i tried.
filehandler.pl sub read_file { @lines = <>; chomp(@lines); print join (',',@lines) . "\n"; } read_file;
Output:
/filehandler.pl Arsenal Cech,Bellerin,Per,Koscielny,Nacho,Ramsey,Coquelin,Alexis,Ozil,Welbeck, +Giroud
I'm able to print the output on the terminal as comma delimited but i'm stuck as to how to iterate through the o/p and print each element in Array.

Replies are listed 'Best First'.
Re: Reading File Into Array
by ww (Archbishop) on Mar 28, 2016 at 02:46 UTC

    There are multiple issues to deal with here, potentially including list vs scalar context and subtleties re lists vs arrays.... but the following is one method avoiding those, written to be utterly explicit about each step, and using your Arsenal data:

    # filehandler.pl #1158919 use strict; use warnings; use 5.018; sub read_file { my (@names, @out); my @lines = <>; chomp(@lines); for my $line(@lines) { @names = split /,/, $line; for my $name (@names) { push @out, $name; } } for my $item(@out) { print $item . "\n"; } } read_file;

    We could, of course, append the newline after each name at Ln 13, instead of in Ln17, but either would satisfy your desire to "print each element of the array on a newline."

      Thank you for this. I came up with this but the ouput is different to yours. what am i doing wrong here?
      sub read_file { @lines = <>; chomp(@lines) ; @lines = join (',',@lines); for $i(@lines){ print $i . "\n"; } } &read_file;

        Not a cure here, but please, save yourself much future grief, by getting into the habit of using strict and warnings.

        Also, read the FAQs here: good questions show output, desired output and (errors and warnings) if any.


        Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
        1. code
        2. verbatim error and/or warning messages
        3. a coherent explanation of what "doesn't work actually means.
        I think i realised this after your message . What i was think of as an array was in fact a list ?. I changed my code accordingly and it worked
        sub read_file { @lines = <>; chomp(@lines) ; $oldlines = join (',',@lines); $oldlines =~ s/,/\n/xg; print $oldlines; } &read_file;
Re: Reading File Into Array
by BrowserUk (Patriarch) on Mar 28, 2016 at 03:14 UTC

    Try this; ask questions:

    #! perl -slw use strict; sub readfile { local $/; ## enable slurp mode +; my @lines = map{ split ',', } split "\n", <>; ## split file by lin +es, then lines on commas. return @lines; } my @lines = readfile(); print for @lines; __END__ C:\test>1158919 junk.dat Cech Bellerin Per Koscielny Nacho Ramsey Coquelin Alexis Ozil Welbeck Giroud

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Reading File Into Array
by dasgar (Priest) on Mar 28, 2016 at 02:11 UTC
      Hi, Thanks for the feedback. I actually wanted my output to be like this.
      Cech Bellerin Per Koscielny ..
Re: Reading File Into Array
by GotToBTru (Prior) on Mar 28, 2016 at 16:58 UTC

    You know that your output will have a different number of lines than your input, so you need to deal with the step of turning one input line into multiple output lines.

    sub read_file { @lines = <>; chomp(@lines); for $line (@lines) { print join "\n", split ',', $line; print "\n" } } read_file;
    $: perl 1158919.pl < Arsenal Cech Bellerin Per Koscielny Nacho Ramsey Coquelin Alexis Ozil Welbeck Giroud $:
    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-05-29 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found