Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: find common data in multiple files

by Discipulus (Monsignor)
on Dec 28, 2017 at 10:20 UTC ( #1206314=note: print w/replies, xml ) Need Help??

in reply to find common data in multiple files

Hello mao9856,

Have you run the code you posted? @counts and even more open my A, "<", "@counts "could not open file1 $!"; make no sense at all. Also foreach (@ARGV) makes no sense infact you are never using $_ the implicit variable filled for you by foreach when you do not specify a named variable.

So the beginning of your program must be better wrote as:

use strict; use warnings; my %result; # my @counts=(); # no need of this foreach (@ARGV) { # my $column=0; # no need of this too open my $fh, "<", $_ or die "could not open file [$_] $!"; while (<$fh>) { chomp; ...

Use print to debug your program: print "DEBUG: working on file [$_]\n" as first line of the loop will confirm you are reading all files.

Then you do not need to split you lines as you want to check for ID121 ABC14 whole presence in every file. So put the whole line as key of the results hash and ++ it as you are doing.

A tip: as you need only strings that are in all files add every entry of the first file and then for following files just ++ keys that are already present in the hash.


There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: find common data in multiple files
by mao9856 (Sexton) on Dec 29, 2017 at 07:54 UTC

    Hello Discipulus I haven't tried the code, but I wrote it based on my understanding. Please excuse me i am very beginner in perl. my @counts=() was wriiten to define number of files so while running this programme i thought I would use: perl *.txt And ARGV was used because name of files vary. As per suggestions, i am trying to run new code. Thank you for suggestions:)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1206314]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-06-19 14:57 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.