spln2016:diario1
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
spln2016:diario1 [2016/02/18 21:00] – [Diário de Bordo - Aula 1 - 2016-02-15] ambs | spln2016:diario1 [2016/02/18 21:58] – [Diário de Bordo - Aula 1 - 2016-02-15] ambs | ||
---|---|---|---|
Line 46: | Line 46: | ||
use utf8::all; # para usar o encoding utf8 para todos os inputs | use utf8::all; # para usar o encoding utf8 para todos os inputs | ||
+ | === Exemplo de declaração de uma variável === | ||
+ | my $y = 20; # my é utilizado para declarar variáveis locais | ||
+ | # para var globais utiliza-se o our | ||
+ | print " | ||
+ | === Contar as linhas de um input === | ||
- | # --------------------------------------- | + | my ($l,$c); |
+ | while ($l = <>) { | ||
+ | # <> | ||
+ | # já < | ||
+ | | ||
+ | } | ||
+ | |||
+ | print "$c linhas\n"; | ||
- | my $y = 20; #my é utilizado para declarar variáveis locais, para var globais utiliza-se o our | ||
- | print " | ||
+ | === Exercicío com a var $_ === | ||
- | + | | |
- | + | my ($c); | |
- | # --------------------------------------- Contar as linhas de um input ----------------------------------------------- | + | while (<> |
- | + | ||
- | my ($l,$c); | + | |
- | while ($l = <>){ #<> - operador diamante vai ler o input, o diamante vazio permite receber ficheiros, já < | + | |
- | c++; | + | |
- | } | + | |
- | + | ||
- | print "$c linhas\n"; | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | # --------------------------------------- Exercicío com a var $_ ----------------------------------------------------- | + | |
- | + | ||
- | #se não guardarmos o que é lido em nenhuma variável ele vai para a var $_ | + | |
- | my ($c); | + | |
- | while (<> | + | |
print $_; | print $_; | ||
- | | + | |
| | ||
- | } | + | |
- | + | ||
- | print "$c linhas\n"; | + | print "$c linhas\n"; |
- | + | ||
- | + | ||
- | # --------------------------------------- Contar palavras -------------------------------------------------------------- | ||
- | my ($c); | + | === Contar palavras === |
- | while (<> | + | |
- | while ($_ =~ /\w+/g) { #utilizamos expressões regulares para identificar o que é uma palavra, as letros inseridas depois das barras são modificadores, | + | |
- | $c++; | + | |
- | } | + | |
- | } | + | |
- | print "$c palavras\n"; | + | my ($c); |
+ | while (<>) { | ||
+ | while ($_ =~ /\w+/g) { | ||
+ | # utilizamos expressões regulares para identificar o que é uma palavra, | ||
+ | # as letras inseridas depois das barras são modificadores, | ||
+ | # neste caso o g é utilizado para fazer o match à palavra | ||
+ | # quantas vezes forem possiveis na string | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | |||
+ | |
spln2016/diario1.txt · Last modified: 2016/02/18 21:58 by ambs