Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://902129]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-05-21 03:56 GMT
Find Nodes?
    Voting Booth?