read lines into aray


We have not learned about arrays yet but let me show what happens when we put the result of the slurp() call into an array. all the content of the file ends up in as the first and only element of the array. If you want to read every line into a separate element of the array you need to use the lines() function.

examples/files/read_file_into_array.p6
#!/usr/bin/perl6
use v6;

my $filename = 'examples/files/slurp_file.p6';

# reads all the content of the file in the first element of the array!
my @content = slurp $filename;
say @content.elems;

my @rows = lines $filename;
say @rows.elems;

Copyright 2006, 2007, 2008, 2009, 2010 Gabor Szabo http://szabgab.com/ Index | TOC
If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed