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

serious self confusion over line counts and hashs.

Thread Next
From:
Harry Putnam
Date:
April 25, 2010 12:25
Subject:
serious self confusion over line counts and hashs.
Message ID:
878w8bibce.fsf@newsguy.com
I've managed to confuse myself thoroughly working on a project.

I hoped to read a list of -f type files into a hash of
File::Find::name and $_.

But kept coming out with different counts than I get with shell find. 

I tried writing a script that tested things various ways, but can't
really reconcile the differences I see.

I've been tinkering endlessly here so there may some claptrap in the
script. 

First the output, then the script below it:

deleting ./h1
deleting ./h2
deleting ./ckarr
deleting ./nf
    -------    -------
   ./h1:
         lines: 631
          size: 32768
    -------    -------
   ./h2:
         lines: 485
          size: 24576
    -------    -------
   ./ckarr:
         lines: 623
          size: 20480
    -------    -------
   ./nf:
         lines: 628
          size: 32768
    -------    -------
-------        ---------       ---=---       ---------      -------- 
Now from cmd line:
reader > wc -l h1 h2 ckarr nf
   647 h1
   565 h2
   647 ckarr
   647 nf
  2506 total

-------        ---------       ---=---       ---------      -------- 

#!/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";
}

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";
}

close $h1fh;
close $h2fh;
close $ckfh;


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