slurp
Exercise: Print sum of numbers
read lines into arayWe 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 |
Follow me: