in reply to How do I read the contents of a file?
To the members or future members of the Portuguese language.
Como é de normal encontramos em Perl diversas maneiras de fazer uma mesma rotina vou colocar de maneira bem simples, como construir uma função de leitura de uma arquivo, onde será retornado um array, onde cada posição representa um linha do arquivo.
As it is common in Perl to find several ways to do the same thing, I will demonstrate a very simple way to construct a file reading function where an array is returned in which each element represents one line of the file.
sub le { my $nomeArq = $_[0]; # recebe com parâmetro o nome do arquivo. my @arrayArq; my $linha; my $num = 0; open (ARQ, "< $nomeArq") or die "Erro ao abrir o arquivo: $nomeArq +: $!"; # Abre o arquivo ou mata o processo com uma mensagem de erro. while ($linha = <ARQ>) { # Varre todas as linhas do arquivo. $arrayArq[$num] = $linha; # Insere linha no array. $num++; } close ARQ; # Fecha o Arquivo return @arrayArq; }
I would like to point out that this is an example simple enough for a beginner to easily understand how it works.
É natural que a mesma seja melhorada, para sua efetiva utilização.
Naturally this can be adjusted to better fit a particular use.
English translation by QandAEditors.
|
|---|