
Chris Charley
User
Nov 23, 2011, 11:36 AM
Post #15 of 21
(1496 views)
|
|
Re: [mykolg] Using search and replace
[In reply to]
|
Can't Post
|
|
Funny, I got the same message, Use of uninitialized value $firstWord in print at t2.pl line 35. but I can't see why exactly with your code. Here is my code that did what you intended (I believe). #!/usr/bin/perl use warnings; use strict; my $bookTitle = "anna.book"; my $out_file = "anna.csv"; open BOOK, "<", $bookTitle or die "Could not open $bookTitle!. $!"; open FILE, ">", $out_file or die "Could not open $out_file for writing. $!"; my $posID = 1; { local $/ = ""; while (<BOOK>) { chomp; my ($first_word) = /(\w+)/; my $word_count = () = /\w+/g; my $line_count = s/\n/ /g || 0; print FILE join(",", $posID++, $first_word, $word_count, $line_count, "\"$_\""), "\n"; } } close BOOK or die "Could not close $bookTitle. $!"; close FILE or die "Could not close $out_file. $!"; I got output like: 1,Chapter,2,0,"Chapter 1" 2,Happy,14,1,"Happy families are all alike; every unhappy fa 3,Everything,200,17,"Everything was in confusion in the Oblo 4,Three,102,8,"Three days after the quarrel, Prince Stepan A 5,Yes,81,7,""Yes, yes, how was it now?" he thought, going ov 6,Stepan,156,13,"Stepan Arkadyevitch's eyes twinkled gaily, 7,Ah,39,3,""Ah, ah, ah! Oo!..." he muttered, recalling ever 8,Yes,65,5,""Yes, she won't forgive me, and she can't forgiv 9,Most,67,5,"Most unpleasant of all was the first minute whe 10,She,39,3,"She, his Dolly, forever fussing and worrying ov 11,What,10,0,""What's this? this?" she asked, pointing to th 12,And,34,2,"And at this recollection, Stepan Arkadyevitch, 13,There,96,9,"There happened to him at that instant what do 14,This,47,4,"This idiotic smile he could not forgive himsel 15,It,15,1,""It's that idiotic smile that's to blame for it 16,But,21,1,""But what's to be done? What's to be done?" he 17,Chapter,2,0,"Chapter 2" 18,Stepan,214,18,"Stepan Arkadyevitch was a truthful man in 19,Oh,155,12,""Oh, it's awful! oh dear, oh dear! awful!" Ste 20,There,73,6,"There was no solution, but that universal sol 21,Then,107,9,""Then we shall see," Stepan Arkadyevitch said 22,Are,20,2,""Are there any papers from the office?" asked S 23,On,30,2,""On the table," replied Matvey, glancing with in 24,Stepan,49,4,"Stepan Arkadyevitch made no reply, he merely 25,Matvey,24,2,"Matvey put his hands in his jacket pockets, 26,I,27,2,""I told them to come on Sunday, and till then not 27,Stepan,37,3,"Stepan Arkadyevitch saw Matvey wanted to mak 28,Matvey,31,2,""Matvey, my sister Anna Arkadyevna will be h 29,Thank,40,3,""Thank God!" said Matvey, showing by this res PS line_count is off by one (since I chomped off the newline at the end of each paragraph).
(This post was edited by Chris Charley on Nov 23, 2011, 11:39 AM)
|