Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: how to search for a string in a particular file?

by bart (Canon)
on May 29, 2012 at 08:03 UTC ( #972947=note: print w/replies, xml ) Need Help??

in reply to how to search for a string in a particular file?

This sounds like a homework problem, so I won't help too much on the simple code. However, your step 2 is still a bit too vague, and large. You'll have to split it up some more into subtasks:
  1. Read the data from the file into a string variable
  2. Search in the variable for the particular string
Depending on the size of the file, and of the type of string you search for, you may slurp it all in at once, or read it in chunks, for example one line at a time, and search in each line in turn. This won't work well if the string you search for contains a newline anywhere other than at its end... Nowadays, computers have a lot of memory, and even largish files (megabytes) are "small" compared to the amount of memory available. So reading it all in one chunk is nowadays a decent approach.

Now, how can you search for a string? You can use a regex, or index. For simple literal strings, and not patterns, index is the easier approach.

  • Comment on Re: how to search for a string in a particular file?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://972947]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-05-20 14:26 GMT
Find Nodes?
    Voting Booth?