Er1kW has asked for the wisdom of the Perl Monks concerning the following question:

I everyone. I am at a brick wall. I am attempting to match a pattern that is inconsistant in a large file.

Back ground. This is a circuit report that is generated to a flat file. The report wraps the circuit name to the next line if it goes beyone it's parameters.

Example: Circuit 12.27 ABC WIRELESS nnn-n |Up |VCC | | nnn | | | | ABC1 | |50.1 |12|ON ABC1 | |50.2 | 3|Off

This is shortened for readability but the beginning is what I am looking at.

In this file all circuits are represented in either 3 or 4 lines.

Circuit Name line is either followed by the rest of circuit name/number or ABC1|ABC2.

What I need is to join second 1/2 of Circuit name on line two to line one circuit name.

The first line always starts at ^
the second line (if wrapped) starts at the fifth space from ^
the third and fourth line (ABC1|ABC2) starts at the second space from ^

or if not wrapped first line starts at ^
second and third start at the seond space from ^

any help would be greatly appreciated.

Thank you Lost Boy

Replies are listed 'Best First'.
Re: Look ahead and comp if pattern matches next line
by chromatic (Archbishop) on Apr 28, 2001 at 03:29 UTC
    There are several ways to handle this. Without seeing your code, I can only guess at what you've tried.

    One approach is to read more than a line at a time. You could set $/ to read in a record at a time, which is made easier if there's a blank line or some other marker between records.

    Another approach is to read the entire file into a scalar, by localizing $/ within a block, reading the file into a scalar, and then using multi-line regexes.

    You could read the entire file into an array, loop through with an index variable, incrementing the index as necessary, or building up a second array of lines.

    Similarly, you could loop through a line at a time, building a single array, appending a continued line.

    Depending on how large the file is, you might want to continue processing a line at a time. I would probably do something like this pseudo-code:

    # open file # declare a last_line variable # loop, reading a line at a time # check to see if line is continued # if so, append the current item to the last_line variable # set a flag that we've done this # process last_line (in another function, probably) # stick the current line in last_line # if the flag is set, empty out last_line
    This depends on a lot of specifics, but you get to process (basically) a line at a time for the small overhead of keeping a little bit of state information around.
Re: Look ahead and comp if pattern matches next line
by Chmrr (Vicar) on Apr 28, 2001 at 03:16 UTC
    I'd suggest walking through the file one line at a time. Some thing along the lines of the following (I don't know the sort of data structure you'd be using):
    my @records = (); my %current = (); while (<FILE>) { my @line = split /\s*\|/; if ($line[0] =~ /^\S/) { if (keys %current) { push @records, {%current}; %current = (); } @current{qw/name var1 var2 var3/} = @line; } elsif ($line[0] =~ /^ (.*)/) { $current{name} .= $1; } else { # Deal with the other lines as you need to.. } } push @records, {%current};
    Hope this helps.

     
    perl -e 'print "I love $^X$\"$]!$/"#$&V"+@( NO CARRIER'

      This gives me a start.
      I will play with it over the weekend.
      I am attempting to make it 3 lines of data per circuit.
      So just need to add the first section of the extra line up to the previous line.

      This ends up being a flat file split by : for later use
      in a web based reporting tool
      The problem I am having right now is they are unable to
      search some circuits because the original reporting tool
      truncated the extra onto the next line

      Thank you again at least now I have a little bit to chew on
      Er1KW