Hi Rob,

I wasn't quite sure at first what you meant by passing the file handle
<DATA> in the while loop when $fh already existed,
so I changed the code slightly like this:


my $file = "file.txt";
open(my $fh, "<", $file) or die $!;

while (<$fh>) {

Works like a charm, thanks again!

Regards,
Wernher

>
> Rob
>
>
> use strict;
> use warnings;
>
> use Fcntl 'SEEK_SET';
>
> my $format;
>
> my $fh = *DATA;  # Replace with the appropriate 'open my $fh, '<', ... or die 
> $!;
>
> # Remember the where the file begins, and then build the unpack pattern from
> # the first line continaining only hyphens and underscores
> #
> my $bof = tell $fh;
> while (<DATA>) {
>  chomp;
>  if (tr/- //c == 0 and tr/-// > 0) {
>    while (/-+/g) {
>      my ($beg, $len) = ($-[0], $+[0] - $-[0]);
>      $format .= "\@$beg A$len ";
>    }
>    last;
>  }
> }
>
> warn qq(Data will be upacked with a format of "$format"\n\n);
>
> # Now rewind to the beginning of the file, ignore anything that contains only
> # whitespace, and unpack every record according to the pattern that we just 
> built
> #
> seek $fh, $bof, SEEK_SET;
> while (<$fh>) {
>  next unless /\S/;
>  my @data = unpack $format;
>  print join ',', @data;
>  print "\n";
> }
>
>
> __DATA__
>
>
> CTX   Destination                                                             
> Enabled   Connection         Sync'ed-as-of-time
> ---   ---------------------------------------------------------------------   
> -------   ----------------   ------------------
> 1     pool://ZABRYDD01.localdomain/RDP_NEW_REP                                
> yes       Sat Jun 18 08:56   Fri Jun 24 06:37
> 2     dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101    
> yes       Sat Jun 18 08:57   Fri Jun 24 08:01
> 4     dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22     
> yes       Sat Jun 18 08:57   Fri Jun 24 09:00
> 5     pool://ZARDPDD01.localdomain/BRYREP                                     
> yes       Sat Jun 18 08:57   Fri Jun 24 07:01
> 8     dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31     
> yes       Sat Jun 18 08:57   Fri Jun 24 09:00
> 10    dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421    
> yes       Sat Jun 18 08:57   Fri Jun 24 09:00
> 12    dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5      
> yes       Sat Jun 18 08:57   Fri Jun 24 09:00
> 13    dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60     
> yes       Sat Jun 18 08:57   Fri Jun 24 09:00
> ---   ---------------------------------------------------------------------   
> -------   ----------------   ------------------
>
> **OUTPUT**
>
> Data will be upacked with a format of "@0 A3 @6 A69 @78 A7 @88 A16 @107 A18 "
>
> "CTX","Destination","Enabled","Connection","Sync'ed-as-of-time"
> "---","---------------------------------------------------------------------","-------","----------------","------------------"
> "1","pool://ZABRYDD01.localdomain/RDP_NEW_REP","","Sat Jun 18 08:56","Fri Jun 
> 24 06:37"
> "2","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test101","yes","Sat
>  Jun 18 08:57","Fri Jun 24 08:01"
> "4","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test22","yes","Sat
>  Jun 18 08:57","Fri Jun 24 09:00"
> "5","pool://ZARDPDD01.localdomain/BRYREP","yes","Sat Jun 18 08:57","Fri Jun 
> 24 07:01"
> "8","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test31","yes","Sat
>  Jun 18 08:57","Fri Jun 24 09:00"
> "10","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test421","yes","Sat
>  Jun 18 08:57","Fri Jun 24 09:00"
> "12","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test5","yes","Sat
>  Jun 18 08:57","Fri Jun 24 09:00"
> "13","dir://ZARDPDD01.localdomain/backup/ZABRYDD01REP/linux/backup/test60","yes","Sat
>  Jun 18 08:57","Fri Jun 24 09:00"
> "---","---------------------------------------------------------------------","-------","----------------","------------------"
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to