arajani has asked for the wisdom of the Perl Monks concerning the following question:
I'm reading a file (each line has many fields enclosed by double quotes). I want to count the number of double quotes in each line. The fields are separated by commas(,) but I cannot use this as separator as field also may have the comma as part of its string. So to cross check the number of fields in a record (line) I am counting the number of double quotes. So far good. I have the following code, which is hanging. Syntax seem to be ok, but still this is a problem. Any suggestions ?
The file abc.txt has following data (just 2 lines)open(FILENAME,"c:\\abc.txt") || die "Cannot Open the file.\n" ; my $quote_count = 0 ; my $pos = 0 ; my $record = "" ; while(<FILENAME>) { $record = $_ ; chomp $record ; while ($pos = index($record, '"', $pos) != -1) { $quote_count++; $pos++; } } print "Number of double quotes are $quote_count\n"; close FILENAME ;
"11111","xxxx","yyyy","222, world drive" "22222","abc","jkl","my road, my world"What is causing the hanging of the process ? pls let me know.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Counting number of double quotes in a string
by japhy (Canon) on May 10, 2002 at 22:26 UTC | |
by arajani (Novice) on May 13, 2002 at 16:54 UTC | |
|
Re: Counting number of double quotes in a string
by thelenm (Vicar) on May 10, 2002 at 22:23 UTC | |
|
Re: Counting number of double quotes in a string
by mephit (Scribe) on May 11, 2002 at 03:28 UTC |