develooper Front page | perl.beginners | Postings from April 2010

Re: serious self confusion over line counts and hashs.

Thread Previous | Thread Next
From:
Shawn H Corey
Date:
April 25, 2010 13:14
Subject:
Re: serious self confusion over line counts and hashs.
Message ID:
4BD4A28B.7060103@gmail.com
Harry Putnam wrote:
> #!/usr/local/bin/perl
> 
> 
> use strict;
> use warnings;
> use File::Find;
> use Cwd;
> 
> my %h1;
> my %h2;
> my $targ = shift;
> my @ckarr;
> 
> my $h1ff = './h1';
> my $h2ff = './h2';
> my $ckff = './ckarr';
> my $nf = './nf';
> 
> for($h1ff,$h2ff,$ckff,$nf){
>   if (-f $_){
>     print "deleting $_\n";
>     unlink $_ or die "Can't  unlink $_ : $!";
>   }
> }
> 
> open my $nfh,'>',$nf or die "Cannot open $nf: $!";
> 
> find({ wanted =>
>       sub {
>         my $dir = getcwd;
>         if(-f $dir . '/' . $_) {
>           print $nfh "V $File::Find::name K $_\n";
>           $h1{$File::Find::name} = $_;
>           $h2{$_} = $File::Find::name;
>           push @ckarr, $File::Find::name;
>         }
>      },no_chdir =>0,
>    },
>    $targ
>  );
>  open my $h1fh,'>',$h1ff or die "Can't open $h1ff: $!";
>  open my $h2fh,'>',$h2ff or die \"Cannot open $h2ff: $!"; 
>  open my $ckfh,'>',$ckff or die \"Cannot open $ckff: $!"; 
> 
> foreach my $key (keys %h1){
>   print $h1fh "V $h1{$key} K $key\n"; 
> }
> 
> foreach my $key (keys %h2){
>   print $h2fh "V $h2{$key} K $key\n"; 
> }
> 
> for(@ckarr){
>    print $ckfh "$_\n";
> }
> 

close $h1fh or die "could not close $h1: $!\n";
close $h2fh or die "could not close $h2: $!\n";
close $ckfh or die "could not close $ckarr: $!\n";

> print "    -------    -------\n";
> for($h1ff,$h2ff,$ckff,$nf){
>   if (-f $_){
>     my $file = $_;
>     my $sz = (stat($_))[7];
>     open my $fh,'<', $_ or die "Can't open $_: $!"; 
>     my $lines;
>     while (<$fh>) {
>        $lines = $.;
>     }
>     print "   $file:
>          lines: $lines
>           size: $sz\n";
>     close $fh;
>   }
>   print "    -------    -------\n";
> }

Output is buffered.  The files have to be closed for the last lines to 
be printed to the file.


-- 
Just my 0.00000002 million dollars worth,
   Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy:  use only FLOSS.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About