Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: deleting special lines from files

by theroninwins (Friar)
on Sep 02, 2004 at 08:58 UTC ( #387843=note: print w/replies, xml ) Need Help??

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

Thanks but i want to run it from within the program. I program does other things too. It is only part of the program, so i can't call the program like that. But thank it is still a very usefull hint you gave me.
  • Comment on Re^2: deleting special lines from files

Replies are listed 'Best First'.
Re^3: deleting special lines from files
by davorg (Chancellor) on Sep 02, 2004 at 09:02 UTC
    my @data = grep { /^\S+;/ } <FILEHANDLE>;

    Then the lines you want are in @data.


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

      Perfect it works. Thanks so much.
      Just to understand better (it's the only way to learn :-) ) what do those symblos mean with grep?? What would you have to do if for examples the lines would be test;;understand and you want to delete those with the ;; in them??
        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

        The grep symbols are a regular expression operator... its the m// operator with the m omitted (which is very valid and quite common) The regex he posted would not take those out as well. you would need to use an assertion after the semicolon in the current regex... to say... all this.. not followed by a ;.. the match says.
        m/ #Match ^#From the beginning of the line \S+#One or more non space characters ;#followed by a semi-colon /
        Anyone feel free to correct me if I'm wrong there.
Re^3: deleting special lines from files
by graff (Chancellor) on Sep 03, 2004 at 02:50 UTC
    If the "other things" in your program are all happening inside a while loop like this:
    while (<>) { # or maybe using a file handle, like <IN> # do stuff... }
    and you just want to skip lines that start with a semi-colon, then just add one line at the top of the loop like this:
    while (<>) { next if ( /^\s*;/ ); # skip if first non-space char is ";" # now do stuff... }
    If you also want to delete lines with double semi-colons (because these represent empty fields), adjust that first line within the loop as follows:
    while (<>) { next if ( /^\s*;/ or /;;/ ); # now do stuff... }
    The "next" statement simply causes the script to skip everything that follows in the loop, and go right to the next iteration. The regular expressions in the "if" statement are saying:
    ^ -- line begins with... \s* -- zero or more white-space characters ; -- followed by a semi-colon or ;; -- there are two semi-colons together anywhere in the line

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-05-29 11:34 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.