1 : #!/usr/local/bin/perl?????????
2 :
3 : while ($inputline = <STDIN>) {
4 : while ($inputline =~ /\b[A-Z]\S+/g) {
5 : $word = $&;
6 : $word =~ s/[;.,:-]$//; # remove punctuation
7 : for ($count = 1; $count <= @wordlist;
8 : $count++) {
9 : $found = 0;
10: if ($wordlist[$count-1] eq $word) {
11: $found = 1;
12: $wordcount[$count-1] += 1;
13: last;
14: }
15: }
16: if ($found == 0) {
17: $oldlength = @wordlist;
18: $wordlist[$oldlength] = $word;
19: $wordcount[$oldlength] = 1;
20: }
21: }
22: }
23: print ("Capitalized words and number of occurrences:\n");
24: for ($count = 1; $count <= @wordlist; $count++) {
25: print ("$wordlist[$count-1]: $wordcount[$count-1]\n");
26: }
Here is a line of Input.?????????????????????????????????????????????????????????????????????????????????????$word????????????????????????????????????7~15??????@wordlist???????????????????????$word????@wordcount???????????????????????????????????@wordlist??????????$word????16~20??@wordlist??@wordcount?????????????
This Input contains some Capitalized words.
^D
Capitalized words and number of occurrences:
Here: 1
Input: 2
This: 1
Capitalized: 1
$fruit{"bananas"}???????????????
$number{3.14159}
$integer{-7}
1 : #!/usr/local/bin/perl?????????
2 :
3 : while ($inputline =) {
4 : while ($inputline =~ /\b[A-Z]\S+/g) {
5 : $word = $&;
6 : $word =~ s/[;.,:-]$//; # remove punctuation
7 : $wordlist{$word} += 1;
8 : }
9 : }
10: print ("Capitalized words and number of occurrences:\n");
11: foreach $capword (keys(%wordlist)) {
12: print ("$capword: $wordlist{$capword}\n");
13: }
Here is a line of Input.???????????????????????????????????????????20???????7??
This Input contains some Capitalized words.
^D
Capitalized words and number of occurrences:
This: 1
Input: 2
Here: 1
Capitalized: 1
foreach $capword (sort keys(%wordlist)) {?]????????????
print ("$capword: $wordlist{$capword}\n");
}
???????????????x????Perl5???????"=>"??","??????????????"=>"????????????????????????apples????????17 ???bananas????????9 ???oranges????????none
1: #!/usr/local/bin/perl?????????
2:
3: $inputline = <STDIN>;
4: $inputline =~ s/^\s+|\s+\n$//g;
5: %fruit = split(/\s+/, $inputline);
6: print ("Number of bananas: $fruit{\"bananas\"}\n");
oranges 5 apples 7 bananas 11 cherries 6??????????
Number of bananas: 11
1?????????delete???????????????????????????????????????????????????
2???????????????????????????push??pop??shift??splice????????????????????
%fruit = ("apples", 9,
"bananas", 23,
"cherries", 11);
@fruitsubs = keys(%fruits);
????@fruitsubs??????apples??bananas??cherries??????????????????????????????????????????????sort()??????
%fruit = ("apples", 9,
"bananas", 23,
"cherries", 11);
@fruitvalues = values(%fruits);
????@fruitvalues????????(9,23.11)?????????????foreach $holder (keys(%records)){Perl????????????????????????????each()???
$record = $records{$holder};
}
%records = ("Maris", 61, "Aaron", 755, "Young", 511);each()??????????????????????????????????????????????????????????????
while (($holder, $record) = each(%records)) {
# stuff goes here
}
%words = ("abel", "baker",
"baker", "charlie",
"charlie", "delta",
"delta", "");
$header = "abel";
????????????$header?????????????????????????????????????????????baker????????????????????????1 : #!/usr/local/bin/perl?????????
2 :
3 : # initialize list to empty
4 : $header = "";
5 : while ($line = <STDIN>) {
6 : # remove leading and trailing spaces
7 : $line =~ s/^\s+|\s+$//g;
8 : @words = split(/\s+/, $line);
9 : foreach $word (@words) {
10: # remove closing punctuation, if any
11: $word =~ s/[.,;:-]$//;
12: # convert all words to lower case
13: $word =~ tr/A-Z/a-z/;
14: &add_word_to_list($word);
15: }
16: }
17: &print_list;
18:
19: sub add_word_to_list {
20: local($word) = @_;
21: local($pointer);
22:
23: # if list is empty, add first item
24: if ($header eq "") {
25: $header = $word;
26: $wordlist{$word} = "";
27: return;
28: }
29: # if word identical to first element in list,
30: # do nothing
31: return if ($header eq $word);
32: # see whether word should be the new
33: # first word in the list
34: if ($header gt $word) {
35: $wordlist{$word} = $header;
36: $header = $word;
37: return;
38: }
39: # find place where word belongs
40: $pointer = $header;
41: while ($wordlist{$pointer} ne "" &&
42: $wordlist{$pointer} lt $word) {
43: $pointer = $wordlist{$pointer};
44: }
45: # if word already seen, do nothing
46: return if ($word eq $wordlist{$pointer});
47: $wordlist{$word} = $wordlist{$pointer};
48: $wordlist{$pointer} = $word;
49: }
50:
51: sub print_list {
52: local ($pointer);
53: print ("Words in this file:\n");
54: $pointer = $header;
55: while ($pointer ne "") {
56: print ("$pointer\n");
57: $pointer = $wordlist{$pointer};
58: }
59: }
Here are some words.????????????????
Here are more words.
Here are still more words.
^D
Words in this file:
are
here
more
some
still
words
??3~17???????????4???????????????????$header??????????5?????????????????????7??????????????8?????????????9~15??????????????????????????????????????????????????????13??????????????????14??????????add_word_to_list????????????????????????????? ?????add_word_to_list????????????????? ?????print_list?????????????
foreach $word (sort keys(%wordlist)) {??????????p???????????????????????????^
# print the sorted list, or whatever }
struce{????????????????????????????????????????field1??field2??field3???
int field1;
int field2;
int field3; }mystructvar;
%mystructvar = ("field1" , "" ,??????C????????????????????????%mystrctvar???????????????field1??field2??field3??????????????????????????????????????????????
"field2" , "" ,
"field3" , "" ,);
???????????????????????????????????????????????????left??right???????????alphaleft??alpharight???alpha??????????????????????????????????????????????????????????????????????/????????????/?????????????????/?????? ?????????????ʦ???????????????????? ????????????????????
1 : #!/usr/local/bin/perl??????????
2 :
3 : $rootname = "parent";
4 : %tree = ("parentleft", "child1",
5 : "parentright", "child2",
6 : "child1left", "grandchild1",
7 : "child1right", "grandchild2",
8 : "child2left", "grandchild3",
9 : "child2right", "grandchild4");
10: # traverse tree, printing its elements
11: &print_tree($rootname);
12:
13: sub print_tree {
14: local ($nodename) = @_;
15: local ($leftchildname, $rightchildname);
16:
17: $leftchildname = $nodename . "left";
18: $rightchildname = $nodename . "right";
19: if ($tree{$leftchildname} ne "") {
20: &print_tree($tree{$leftchildname});
21: }
22: print ("$nodename\n");
23: if ($tree{$rightchildname} ne "") {
24: &print_tree($tree{$rightchildname});
25: }
26: }
grandchild1????????????????????
child1
grandchild2
parent
grandchild3
child2
grandchild4
