Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Another way to do this is using grep:

# This code is untested. open PAGE, "$data_reg" or die "Can't open $data_reg: $!"; my @matchinglines = grep { $username eq (split "\t", $_)[0]; } <PAGE>; (print "<div>Unfortunately we cannot find reference to the username supplied.</div> <div>Please check with the webmaster of this site." and exit) unless @matchinglines; close PAGE;

The advantage of this way is that the code is shorter and less convoluted, thus easier to follow. The disadvantage is that it slurps the whole file into a list, so if the file is really huge it can be a memory overhead problem.

I can get round this by taking the 'else' statement out and doing (after close PAGE) [...] But I don't want to!

You didn't say why it is that you don't want to put the error-handling outside the loop. Did you realise that the way you have the code, the error handling happens after each line? So, for each line in the file that doesn't match the supplied username, the error condition would be triggered. (I say "would be", because the exit stops it after the first time.) Why is it that this is preferable to having the error condition checked for just once, after all the lines have been read?

Also, when you do check++, does your real code do something else, like save the line, or is it really just a check? Because, if it's really just a binary check, you could save time if you skip reading the rest of the lines. I don't know how much performance matters for your application, so this is just a thought.

$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

In reply to Re: searching a file by jonadab
in thread searching a file by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2023-05-28 19:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found