開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9)の9章(正規表現によるテキスト処理), 9.6(練習問題)3を解いてみる。
ファイル、sample_text
this story is rendered all in lowercase so that you can search for fred and barney in lowercase, as we usually do. some lines mention a name like fred more than once so that you can search for repeated words, for example. (don't actually try to read this story, which is terribly inane, or you'll be disappointed. we promise.) - - - - - - - - - - - - - - - - - - - - - - - - - - - - one evening, fred and barney were getting ready to go bowling. wilma saw this and said to betty, "you should ask barney to take you out to the hard rock cafe for pterodactyl burgers." betty agreed. "barney and fred never think about us. we should make it a double date." "do you want to bring pebbles and bamm-bamm along?" "sure," said betty. "they always like to play skee-ball." so wilma found fred and told him, "hey, fred, you've been going out bowling every night this week. betty and i want to go to the hard rock cafe for pterodactyl burgers." "but wilma," said fred, "barney and i need to go tonight. this is the night that our league is pitted against the gravel pit strikers. how about if i take you out on monday night?" that wasn't good enough for wilma. "fred flintstone, if you think i'm going to let you leave me at home with pebbles while you go bowling for the tenth night in a row, you've got another think coming. get cleaned up, because we're going." - meanwhile, at the rubbles' house, barney was having the same discussion with betty. "i can't say no to you betty. but fred said that if we can beat the gravel pit strikers tonight, we'll qualify for the final round next month. the grand prize is a romantic vacation on the rock of gibraltar. i want to win that for you." "oh, barney! you are so sweet. of course you can go bowling. do you really think you'll win?" "well, this is why fred and i have been practicing all week. but you can't tell wilma what's going on, because fred made me promise that he could surprise her." "what are we going to do for tonight?" betty asked. "wilma wants us all to go out to the hard rock cafe. what time do you have to be at the alley?" barney checked the time. "we aren't scheduled to start until half past 8." "that's it, then," said betty. i'll create a distraction at about 8. the alley is just around the corner from the restaurant, so you and fred can sneak over there." "i'll call fred and tell him our plan." - fred had come up with a plan of his own. with his bowling ball in hand, he tiptoed towards the door, keeping an eye on the door to the bathroom to see if wilma was coming. whoops! he tripped over dino's tail. "arf arf arf" yelped the dogosaurus. "what was that?" called wilma from the bathroom. "nothing, nothing, dear," said fred, hurriedly hiding the bowling ball behind his back. "i think dino just saw a mouse." "a mouse! well, i should hope you'll catch it, we can't have any mice around pebbles." "no problem. i've caught it now," lied fred. "i'll just take it outside and release it." as he headed again toward the door, wilma emerged from the bathroom. "what color is it?" she asked. "uhm, black?" fred responded, holding the bowling ball behind his back as he walked backwards towards the door. "betty saw a black mouse last week. i wonder whether this is a relative. let me see it." "no, this one is more of a grey mouse, now that i think of it. no possible relation. no reason for you to look at the mouse." "fred flintstone, you let me see that mouse this minute!" cried wilma. the telephone rang. "you get that, wilma," said fred. "i'll be back in a moment." he ducked outside. wilma answered the phone. "hello?" she asked. "uhm, yes." said barney on the other end of the line, disguising his voice. "this is mister, uh, rarnard bubble calling from the department of, uhm, mineral, uhm, transportation. i'm calling for mister fred flintstone. is mister fred flintstone available to come to the telephone?" at that moment, fred rushed back into the house, having left the bowling ball in the car. "it's for you, fred," said wilma. "mister bubble from the department of mineral transportation." "hello," fred said into the phone, not understanding. "heya, fred," said barney. "betty and i have figured out what to do. she'll distract wilma, and you and i can sneak over to the bowling alley." "yes, yes," said fred. wilma stood by, curious about this conversation. barney continued. "you and wilma go ahead to the restaurant. after you're gone, betty and i will get your bowling ball and bring it with us. where do you keep it?" wilma was getting more curious. fred said, "well, that mineral, i don't think i have any of it around the house right now." "but fred," said barney, "it's a bowling ball, not a mineral." "i'm sure i can't help you with that." "fred, where do you keep your bowling ball? is it in the garage?" "no, none in the garage, i'm sure of it." wilma interrupted him. "fred, give me the phone. i'll take care of this while you shave." she pulled the phone away from fred in time to hear barney say, "no, bamm-bamm, don't play with daddy's bowling ball!" "barney, is that you?" she asked the phone. "yep. i mean," said barney, hastily disguising his voice again, "hello? hello? i think we have a bad connection. i think i just heard someone named barney on the line. but there is no one named barney here. no barney at all. put that bowling ball down, bamm-bamm!" end of story
3.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl use strict; use warnings; my $in = $ARGV[0]; my $out = $in; $out =~ s/(\s.\w+)?$/.out/; open IN, "<$in"; open OUT, ">$out"; while(<IN>){ chomp; s/Fred/\n/gi; s/Wilma/Fred/gi; s/\n/Wilma/g; print OUT "$_\n"; }
入出力結果(Terminal)
$ ./perl_program sample_text $ cat sample_text.out this story is rendered all in lowercase so that you can search for Wilma and barney in lowercase, as we usually do. some lines mention a name like Wilma more than once so that you can search for repeated words, for example. (don't actually try to read this story, which is terribly inane, or you'll be disappointed. we promise.) - - - - - - - - - - - - - - - - - - - - - - - - - - - - one evening, Wilma and barney were getting ready to go bowling. Fred saw this and said to betty, "you should ask barney to take you out to the hard rock cafe for pterodactyl burgers." betty agreed. "barney and Wilma never think about us. we should make it a double date." "do you want to bring pebbles and bamm-bamm along?" "sure," said betty. "they always like to play skee-ball." so Fred found Wilma and told him, "hey, Wilma, you've been going out bowling every night this week. betty and i want to go to the hard rock cafe for pterodactyl burgers." "but Fred," said Wilma, "barney and i need to go tonight. this is the night that our league is pitted against the gravel pit strikers. how about if i take you out on monday night?" that wasn't good enough for Fred. "Wilma flintstone, if you think i'm going to let you leave me at home with pebbles while you go bowling for the tenth night in a row, you've got another think coming. get cleaned up, because we're going." - meanwhile, at the rubbles' house, barney was having the same discussion with betty. "i can't say no to you betty. but Wilma said that if we can beat the gravel pit strikers tonight, we'll qualify for the final round next month. the grand prize is a romantic vacation on the rock of gibraltar. i want to win that for you." "oh, barney! you are so sweet. of course you can go bowling. do you really think you'll win?" "well, this is why Wilma and i have been practicing all week. but you can't tell Fred what's going on, because Wilma made me promise that he could surprise her." "what are we going to do for tonight?" betty asked. "Fred wants us all to go out to the hard rock cafe. what time do you have to be at the alley?" barney checked the time. "we aren't scheduled to start until half past 8." "that's it, then," said betty. i'll create a distraction at about 8. the alley is just around the corner from the restaurant, so you and Wilma can sneak over there." "i'll call Wilma and tell him our plan." - Wilma had come up with a plan of his own. with his bowling ball in hand, he tiptoed towards the door, keeping an eye on the door to the bathroom to see if Fred was coming. whoops! he tripped over dino's tail. "arf arf arf" yelped the dogosaurus. "what was that?" called Fred from the bathroom. "nothing, nothing, dear," said Wilma, hurriedly hiding the bowling ball behind his back. "i think dino just saw a mouse." "a mouse! well, i should hope you'll catch it, we can't have any mice around pebbles." "no problem. i've caught it now," lied Wilma. "i'll just take it outside and release it." as he headed again toward the door, Fred emerged from the bathroom. "what color is it?" she asked. "uhm, black?" Wilma responded, holding the bowling ball behind his back as he walked backwards towards the door. "betty saw a black mouse last week. i wonder whether this is a relative. let me see it." "no, this one is more of a grey mouse, now that i think of it. no possible relation. no reason for you to look at the mouse." "Wilma flintstone, you let me see that mouse this minute!" cried Fred. the telephone rang. "you get that, Fred," said Wilma. "i'll be back in a moment." he ducked outside. Fred answered the phone. "hello?" she asked. "uhm, yes." said barney on the other end of the line, disguising his voice. "this is mister, uh, rarnard bubble calling from the department of, uhm, mineral, uhm, transportation. i'm calling for mister Wilma flintstone. is mister Wilma flintstone available to come to the telephone?" at that moment, Wilma rushed back into the house, having left the bowling ball in the car. "it's for you, Wilma," said Fred. "mister bubble from the department of mineral transportation." "hello," Wilma said into the phone, not understanding. "heya, Wilma," said barney. "betty and i have figured out what to do. she'll distract Fred, and you and i can sneak over to the bowling alley." "yes, yes," said Wilma. Fred stood by, curious about this conversation. barney continued. "you and Fred go ahead to the restaurant. after you're gone, betty and i will get your bowling ball and bring it with us. where do you keep it?" Fred was getting more curious. Wilma said, "well, that mineral, i don't think i have any of it around the house right now." "but Wilma," said barney, "it's a bowling ball, not a mineral." "i'm sure i can't help you with that." "Wilma, where do you keep your bowling ball? is it in the garage?" "no, none in the garage, i'm sure of it." Fred interrupted him. "Wilma, give me the phone. i'll take care of this while you shave." she pulled the phone away from Wilma in time to hear barney say, "no, bamm-bamm, don't play with daddy's bowling ball!" "barney, is that you?" she asked the phone. "yep. i mean," said barney, hastily disguising his voice again, "hello? hello? i think we have a bad connection. i think i just heard someone named barney on the line. but there is no one named barney here. no barney at all. put that bowling ball down, bamm-bamm!" end of story $
0 コメント:
コメントを投稿