Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Creating a array hash with all file content

by Anonymous Monk
on Apr 30, 2011 at 05:20 UTC ( #902129=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Guys,appreciate any inputs on this. I want to have an "hash array" of files content Is there a way I can achieve this?

1.Grep for a folder(there are array of folders) in array of file contents

2.Push the file name and the matching grep line into another hash

#!/usr/bin/perl -w use strict; use warnings; my @folders= ("data,power"); my @array=("./data.h","./power.h"); my $folder; my %files_content; my %grep_lines; foreach $file (@array) { open my $fh, $arr or die $!; $files_content{ $file } = <$fh>; close $fh; } foreach $folder (@folders) { foreach $filename (keys %files_content) { push @{ $grep_lines{ $filename } }, grep (/$folder/i, @{ $files_co +ntent{ $filename } }); } }

Comment on Creating a array hash with all file content
Download Code
Re: Creating a array hash with all file content
by Corion (Pope) on Apr 30, 2011 at 06:19 UTC

    Achieving this is easy, but you don't tell us where/how your program fails to do what you want.

    Look at perlop to see how <...> behaves in scalar context. Most likely, you don't want to use it in scalar context.

      Its not printing the grep line and the corresponding line.Appreciate if you can help Coron

        Perhaps you should use a suitable print statement?

Re: Creating a array hash with all file content
by John M. Dlugosz (Monsignor) on Apr 30, 2011 at 10:09 UTC
    $files_content{ $file } = <$fh>;
    That will read the first line of the file.

    If you think you are having problems with the second half, the reporting, start by just using Data::Dumper.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://902129]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-12-25 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls