in reply to Re: Reading File Into Array
in thread Reading File Into Array

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;

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

    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.
Re^3: Reading File Into Array
by Irishboy24 (Sexton) on Mar 28, 2016 at 03:51 UTC
    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;

      I'm still not clear about your understanding of lists and arrays, but this is definitely an improvement. However, please note, the ampersand in the subcall is unnecessary... and unwise. Super Search will reveal many explanations.