Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://972947]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-07-13 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls