in reply to need help in extracting lines

Others have already addressed your explicit question, but I'd like to suggest a different approach to your problem. Of couse, I'm making some assumptions about the real nature of your problem, so correct me if I'm wrong. Or simply disregard. :-)

It appears that your data is a series of chunks separated by empty lines, and that each chunk begins with a Table: ... line. If so, then we could use perl's "paragraph" mode of reading input records:

local $/ = ''; # read paragraphs while (<>) { # each paragraph is multiple lines, the first of which is "Table: .. +." # and the second is some kind of tag enclosed in brackets. my( $table ) = /^Table: (.*)/ or die "Hm... bad paragraph:\n$_"; my( undef, $tag, @lines ) = split /\n/; if ( $tag eq '[Alias]' ) { push @aliases, \@lines; } }
Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.

Replies are listed 'Best First'.
Re^2: need help in extracting lines
by Wiggins (Hermit) on Jan 13, 2009 at 20:46 UTC
    I tried this code out and needed to make a change or 2.
    -- the /^Table regex needs an 's' trailing modifier.
    -- the 'If (tag' condition I changed the 'eq' to '=~', because it turns out that the '[Alias]' line actually ends with a trailing space, and is '[Alias] '.

    But the paragraph mode is great! I once used a customer specific language that did list processing in paragraphs, but that was in the 70's. But it is a great processing mode.

    It is always better to have seen your target for yourself, rather than depend upon someone else's description.
      /^Table regex needs an 's' trailing modifier

      Depends on what you want in the $table variable. I was intending to get the name of the table, i.e. only what follows 'Table:' on that line. Adding the s modifier would put the entire rest of the paragraph into the variable.

      the '[Alias]' line actually ends with a trailing space, and is '[Alias] '

      In that case, I'd write

      if ( $tag eq '[Alias] ' )
      :-)

      But more importantly, square brackets are special in regular expressions, so you'd want to escape them if you go that route:

      if ( $tag =~ /\[Alias\]/ )

      Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.