Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
my @data = grep { /^\S+;/ } <FILEHANDLE>;

grep takes two arguments - a block of code that defines a filter and list of values. It assigns each item in the input list in turn to $_ and then evaluates the block of code. If the block of code returns a true value then the value in $_ is added to the returned list.

So, in this example, each record returned by <FILEHANDLE> is put into $_ and the expression /^\S+;/ is evaluated. /.../ is the match operator (m/.../ without the optional 'm') so the symbols within it are interpreted as a regular expression. By default the match operator matches the regular expression against $_.

So, to recap. Every line from FILEHANDLE is checked against the regular expression. The ones that match are returned by grep.

Next we need to understand what the regular expression is checing for. In this case, it's pretty simple. We are looking for the start of the string (^), followed by one or more non-whitespace characters (\S+), followed by a semicolon (;). If you wanted to check for other types of line then it would just be a case of creating the appropriate regex.

Hope that's clearer.


"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

In reply to Re^5: deleting special lines from files by davorg
in thread deleting special lines from files by theroninwins

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 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 rifling through the Monastery: (3)
As of 2022-06-25 05:35 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (81 votes). Check out past polls.