Un program simplu de numărat cuvintele

106

Secvenţă cod (tst34.pl)

 

Rezultat în consola text

$wordcount = 0;

print("Type a phrase\n");

$line = <STDIN>;

chop ($line);

while ($line ne "") {

     @array = split(/ /, $line);

     $wordcount += @array;

     print("Type a phrase\n");

     $line = <STDIN>;

     chop ($line);

}

 

Type a phrase

It is your turn!

Type a phrase

It is not!

Type a phrase

Oh yes, It is!

Type a phrase

Oh no, It is not!

Type a phrase

 

Total number of words: 16

Un program care inversează ordinea cuvintelor citite de la <STDIN>.

Secvenţă cod (tst35.pl)

 

Rezultat în consola text

@input = <STDIN>;

chop (@input);

# first, reverse the order of the words in each line

$currline = 1;

while ($currline <= @input) {

     @words = split(/ /, $input[$currline-1]);

     @words = reverse(@words);

     $input[$currline-1] = join(" ", @words, "\n");

     $currline++;

}

# now, reverse the order of the input lines and print them

@input = reverse(@input);

print (@input);

 

this is what you think

this is not

^Z

not is this 

think you what is this 

© Cornel Mironel Niculae, 2003-2004

13-Nov-2009