FAQ
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;

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 4 | next ›
Discussion Overview
groupbeginners @
categoriesperl
postedApr 25, '10 at 7:25p
activeApr 25, '10 at 11:46p
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase