Para os membros ou futuros membros da lingua portuguesa.

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; }

Gostaria de lembrar que essa é uma função bem simples para que um iniciante possa entender facilmente como ela funciona.

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.


In reply to Re: How do I read the contents of a file? by Mago
in thread How do I read the contents of a file? by vroom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.