Re: How to split a string read from the file?



pangzhy@xxxxxxxxx wrote:
I want to clear the number and colon at the beginning of each line

Why are you slurping the file into an array (instead of just iterating
over the source file)? Why are you using split instead of a regular
expression? Why are you using all those intermediate variables? Your
task can be made much easier:

#!/usr/bin/perl
use warnings; use strict;

while (<DATA>) { #you would use a handle to your file here
s/^\d+\s*: ?// && print;
}

__DATA__
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;


--
http://DavidFilmer.com

.



Relevant Pages

  • Re: Replace 1st Character On Each Line of a Text File?
    ... You don't have to use an Array. ... Simply open 2 files, the source file ... ForReading and the target file ForWriting. ... When the loop is finished, ...
    (microsoft.public.windows.server.scripting)
  • Re: FAQ 5.28 How can I read in an entire file all at once?
    ... is slower than slurping and looping. ... File::Slurp distribution which compares different slurp styles. ... PS> tie an array to a file so that accessing an element the array ... PS> You can read the entire filehandle contents into a scalar. ...
    (comp.lang.perl.misc)
  • Re: Readline using foreach and while
    ...     print; ... before iterating begins - therefore the values of the new list are ... If there is only a single array, ... consistant if you really think about it. ...
    (comp.lang.perl.misc)
  • Re: Efficient processing of large nuumeric data file
    ... to see how much time is consumed merely by iterating over the ... try/except blocks, mapor int() calls. ... an upper bound on the data, so it seems to me that some kind of array ... I'm not sure an array would net you great savings here, ...
    (comp.lang.python)
  • Re: Programming Puzzle
    ... Easy peasy. ... You can't remove anything from an array. ... Impossible without access into a no key access database. ... merely echoing the actual source file is not ...
    (comp.lang.c)