in reply to Re: MultiLine Tables into Variables
in thread MultiLine Tables into Variables

Dear Moritz,
My understanding of unpack from perlpacktut is that it does a great job for single line position delimited records. I did not see anything for dealing with multi-line records. I do appreciate your pointing me in that direction and I am going to do some research into it.

My understanding is that perlpack can be very strict regarding how the template matches and that is going to be a problem as you can see from my above example that I have no idea if some of the fields stretch 2 or 3 or 4 lines long inside the column.

If you or anyone else can provide anything more I would greatly appreciate it.

Replies are listed 'Best First'.
Re^3: MultiLine Tables into Variables
by moritz (Cardinal) on Aug 14, 2007 at 19:14 UTC
    Well, it doesn't do all the magic for you, but quite a bit:

    #!/usr/bin/perl use warnings; use strict; my (@nodename, @filename, @pathname, @backupdate); use Data::Dumper; { # discard heading line my $tmp = <DATA>; } while (my $line = <DATA>){ chomp $line; my ($nn, $fn, $pn, $bd) = unpack('A8xA9xA8xA15', $line); if ($nn =~ m/\S/){ push @nodename, $nn; push @filename, $fn; push @pathname, $pn; push @backupdate, $bd; } else { $nodename[-1] .= $nn; $filename[-1] .= $fn; $pathname[-1] .= $pn; $backupdate[-1] .= $bd; } } print Dumper([\@nodename, \@filename, \@pathname, \@backupdate]); #1234567890123456789012345678901234567890123 __DATA__ NodeName FileName PathName BackupDate BD3101 bananaswi \breakfa 2007-03-06 ithapple st\fruit 14:02:31.000000 s.gif s\tree\ TP4223 chocolate \sweet\d 2006-02-28 caramelfu esserts\ 21:16:41.000000 dge.gif hersheys\ EO2123 tofuwith \organic\ 2007-07-16 peas.gif vegetable 13:55:06.000000 s\legumes\

    Actually the data would better be stored in a two dimensional array.

    Note that all lines that don't have the Backup Date field need to be padded with whitespaces at the end of the line to be long enough, if that's not the case you'd have to pad them manually before using unpack.