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

Hi Folks,
I have a data file containing:
library_AND PIN.A DELAY_TABLE 1 20 2 40 3 60 4 65 5 75; DELAY_TABLE 1 10 2 15 3 45 4 70 5 80; POWER_TABLE 20 30 40 50 80 97 110 20; POWER_TABLE 3 4 5 6 10 80 90 10 20; library_BUF PIN.B LEAKAGE 1 2 3 4; TRANSIT 1.1 2 3.4;
Given a data file as above, I want to print
library_AND:PIN.A:DELAY_TABLE 1 20 2 40 3 60 4 65 5 75; library_AND:PIN.A:DELAY_TABLE 1 10 2 15 3 45 4 70 5 80; library_AND:PIN.A:POWER_TABLE 20 30 40 50 80 97 110 20; library_AND:PIN.A:POWER_TABLE 3 4 5 6 10 80 90 10 20; library_BUF:PIN.B:LEAKAGE 1 2 3 4; library_BUF:PIN.B:TRANSIT 1.1 2 3.4;
Any suggestions?
Jessica.

Replies are listed 'Best First'.
Re: Need help with line concatenation
by ysth (Canon) on Jan 07, 2008 at 05:52 UTC
Re: Need help with line concatenation
by McDarren (Abbot) on Jan 07, 2008 at 06:00 UTC
    Here is one way:
    #!/usr/bin/perl -l use strict; use warnings; my $first_col = ''; while (<DATA>) { chomp; if (/^library/) { $first_col = $_; } else { print "$first_col:$_"; } } __DATA__ library_AND PIN.A DELAY_TABLE 1 20 2 40 3 60 4 65 5 75; DELAY_TABLE 1 10 2 15 3 45 4 70 5 80; POWER_TABLE 20 30 40 50 80 97 110 20; POWER_TABLE 3 4 5 6 10 80 90 10 20; library_BUF PIN.B LEAKAGE 1 2 3 4; TRANSIT 1.1 2 3.4;

    Cheers,
    Darren :)

      Hi,
      not quite, ++McDarren. The OP asked for some different output.
      ... if (/^library/) { $first_col = $_; my $secnd_col = <DATA>; chomp $secnd_col; $first_col .= ':' . $secnd_col; } ...

      Regards,
      svenXY
Re: Need help with line concatenation
by poolpi (Hermit) on Jan 07, 2008 at 11:17 UTC
    Another idea :
    #!/usr/bin/perl use strict; use warnings; my @header; map { unless (/;$/) { chomp; scalar @header == 2 and undef @header; scalar @header and $_ = ":$_"; push @header, $_; } print @header, ":$_" if /;$/; } <DATA>; __DATA__ library_AND PIN.A DELAY_TABLE 1 20 2 40 3 60 4 65 5 75; DELAY_TABLE 1 10 2 15 3 45 4 70 5 80; POWER_TABLE 20 30 40 50 80 97 110 20; POWER_TABLE 3 4 5 6 10 80 90 10 20; library_BUF PIN.B LEAKAGE 1 2 3 4; TRANSIT 1.1 2 3.4;
    Output:
    library_AND:PIN.A:DELAY_TABLE 1 20 2 40 3 60 4 65 5 75; library_AND:PIN.A:DELAY_TABLE 1 10 2 15 3 45 4 70 5 80; library_AND:PIN.A:POWER_TABLE 20 30 40 50 80 97 110 20; library_AND:PIN.A:POWER_TABLE 3 4 5 6 10 80 90 10 20; library_BUF:PIN.B:LEAKAGE 1 2 3 4; library_BUF:PIN.B:TRANSIT 1.1 2 3.4;

    PooLpi
Re: Need help with line concatenation
by johngg (Canon) on Jan 07, 2008 at 11:52 UTC
    If the data file is not too large you could slurp it into a single string and use a regex to pull out groups of lines with the parts to concatenate in separate memory groups.

    use strict; use warnings; open my $dataFH, q{<}, \ <<END_OF_FILE or die qq{open: $!\n}; library_AND PIN.A DELAY_TABLE 1 20 2 40 3 60 4 65 5 75; DELAY_TABLE 1 10 2 15 3 45 4 70 5 80; POWER_TABLE 20 30 40 50 80 97 110 20; POWER_TABLE 3 4 5 6 10 80 90 10 20; library_BUF PIN.B LEAKAGE 1 2 3 4; TRANSIT 1.1 2 3.4; END_OF_FILE my $dataString = do { local $/; <$dataFH>; }; close $dataFH or die qq{close: $!\n}; my $rxGroup = qr {(?xs) (library_[^\n]+)\n ([^\n]+)\n (.*?\n) (?=(?:library_|\z)) }; while ( $dataString =~ m{$rxGroup}g ) { print qq{$1:$2:$_\n} for split m{\n}, $3; }

    Here's the output.

    library_AND:PIN.A:DELAY_TABLE 1 20 2 40 3 60 4 65 5 75; library_AND:PIN.A:DELAY_TABLE 1 10 2 15 3 45 4 70 5 80; library_AND:PIN.A:POWER_TABLE 20 30 40 50 80 97 110 20; library_AND:PIN.A:POWER_TABLE 3 4 5 6 10 80 90 10 20; library_BUF:PIN.B:LEAKAGE 1 2 3 4; library_BUF:PIN.B:TRANSIT 1.1 2 3.4;

    I hope this is useful.

    Cheers,

    JohnGG

    Update: Removed superfluous my $group = from while clause, left over from earlier version. Original line was

    while ( my $group = $dataString =~ m{$rxGroup}g )