Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^5: deleting special lines from files

by davorg (Chancellor)
on Sep 02, 2004 at 13:10 UTC ( #387896=note: print w/replies, xml ) Need Help??


in reply to Re^4: deleting special lines from files
in thread deleting special lines from files

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.

--
<http://www.dave.org.uk>

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

Replies are listed 'Best First'.
Re^6: deleting special lines from files
by theroninwins (Friar) on Sep 02, 2004 at 13:17 UTC
    OK I got it so far and when I am using the syntax you two gave me it is working, but only in the opposite way it should... I only get the lines with ;; instead of leaving those out and getting the other ones. How can you do that (i.e. do grep everything except those with)
Re^6: deleting special lines from files
by theroninwins (Friar) on Sep 02, 2004 at 13:33 UTC
    I thought that /^\;\;\{2\}/ would work but it doesn't
      Finally I got it. It is :
      my @data = grep { !/\S+;;/ } <SCAN>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-07-06 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?