CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [BillKSmith] Grepping a file in all the files in an array and storing them in a hash: Edit Log



iphone
User

Oct 26, 2010, 9:16 PM


Views: 8036
Re: [BillKSmith] Grepping a file in all the files in an array and storing them in a hash

 
Thanks Bill.I have used read dir and moved ahead,however I am getting the following warning while running my code

Use of uninitialized value in pattern match (m//) at orphan_plf.pl line 37

and line 37 is

Code
$Hash_filematches{$filename}=  grep( /\/\Q$file_name\E#/i, $Hash_filenames{$plf} );


Complete code

Code
#!/usr/bin/perl -w  
use strict;
use warnings;
my %Hash_filenames=();
my %Hash_filematches=();
my $plf;
my $start_dir;
my $start_dir = \\server1\tools ;

my $file_name = "load.c";

opendir(DIR, "$start_dir");
my @plf_files = grep(/\.plf$/,readdir(DIR));
print "PLF FILES\n";
print "@plf_files\n";
foreach my $plf (@plf_files) {
chomp($plf);
open my $match, '<',"$start_dir\\$plf" or die "could not open '$plf' $!";
my @file_lines = <$match>;
#Save the filename as key and the lines in the file as value in a hash
$Hash_filenames{$plf}=@file_lines;
}
closedir(DIR);
if ($file_name) {

foreach my$filename(keys %Hash_filenames) {

#for every key(filename) in the above hash namely #$Hash_filenames
#grep for $file_name in the
#corresponding values (lines in the file)of the file
#and save the match as value and key as filename
#in another hash
$Hash_filematches{$filename}= grep( /\/\Q$file_name\E#/i, $Hash_filenames{$plf} );
}
}
{
local $, = "\n";
print "PRINTING MATCHED HASHES";
foreach my $key ( keys %Hash_filenames ) {
my $value = $Hash_filematches{$key};
print "$key => $value\n";
}
}



(This post was edited by iphone on Oct 26, 2010, 9:33 PM)


Edit Log:
Post edited by iphone (User) on Oct 26, 2010, 9:20 PM
Post edited by iphone (User) on Oct 26, 2010, 9:21 PM
Post edited by iphone (User) on Oct 26, 2010, 9:31 PM
Post edited by iphone (User) on Oct 26, 2010, 9:32 PM
Post edited by iphone (User) on Oct 26, 2010, 9:33 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives