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:
how to compare files by regexp

 



telekomunist
New User

Dec 25, 2009, 12:26 PM

Post #1 of 3 (358 views)
how to compare files by regexp Can't Post

Hello

I have two text files, at one i have some data;

for example:

Introduction
Chapter 1: First Steps In Perl
Chapter 2: Working with Simple Values
Chapter 3: Lists and Hashes
Chapter 4: Loops and Decisions
Chapter 5: Regular Expressions

and the second file which consist data which i would like found at first file using regular expressions

second file example:
Chapter 4
Chapter 3

Respectively i need filter all strings that consist "Chapter 4" or "Chapter 3"

Suppose i need to use hashes, but i have not familiar with its

Just my stupid trying


open(CONTENT,"table.txt");
open(FILTER,"filter.txt");
@aaa = <CONTENT>;
@xxx = <FILTER>;

@hash1{@aaa} = undef;
@hash2{@xxx} = undef;

foreach $key1 (sort(keys %hash1)) {
foreach $key2 (sort(keys %hash2)) {

print if $key1 ~= m/$key2/;
}}


Could you help me, please.

Thank you


7stud
Enthusiast

Dec 25, 2009, 11:31 PM

Post #2 of 3 (351 views)
Re: [telekomunist] how to compare files by regexp [In reply to] Can't Post


Code
 

use strict;
use warnings;
use 5.010;

open(my $LOOK_IN, '<', 'data.txt');
my @lines = <$LOOK_IN>;
close $LOOK_IN;



open(my $LOOK_FOR, '<', 'matches.txt');


while (<$LOOK_FOR>) {
chomp;
my $curr = $_;
my @matches = grep {/$curr/} @lines;

say "--";
for (@matches) {
say;
}
say "-----";
}

close $LOOK_FOR;



(This post was edited by 7stud on Dec 25, 2009, 11:32 PM)


telekomunist
New User

Dec 26, 2009, 9:04 AM

Post #3 of 3 (340 views)
Re: [7stud] how to compare files by regexp [In reply to] Can't Post

It's work
:)
Great thanks to you for your help.

I will more use grep function in perl.

 
 


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

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