in reply to shell commands doesn't integrate with perl
But you're reading the latter files again and again for each search pattern, whereas you shouldn't need to read them more than once. The code you posted would probably work (if there were no problems with "magic" characters in the regex patterns), but if "en_1000" or "HI_1000" happen to contain lines where two or more patterns match, those lines get printed multiple times. Is that your intention?
If so, the following should do the same thing (and I think it will go quicker):
(not tested)#!/usr/bin/perl use strict; use warnings; open( LABELS, "labels" ) or die "labels: $!\n"; my @english; my @hindi; while (<LABELS>) { my ( $eng_indx, $hin_indx ) = map { s/:/|/; $_ } split( /\|/ ); push @english, $eng_indx; push @hindi, $hin_indx; } open( EN, "/home/vikash/pro_1/en_1000" ) or die "en_1000: $!\n"; my @enlines = <EN>; open( HI, "/home/vikash/pro_1/HI_1000" ) or die "HI_1000: $!\n"; my @hilines = <HI>; for my $i ( 0 .. $#english ) { print grep /$english[$i]/, @enlines; print grep /$hindi[$i]/, @hilines; print '*' x 50, "\n"; }
Instead of splitting the English and Hindi "labels" strings on ":", this converts the ":" to "|", so that each label string becomes a single regex with alternations. Then, the "*_1000" files are read only once into memory (this will be a problem if the files are too big).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: shell commands doesn't integrate with perl
by vikashiiitdm (Novice) on Jul 18, 2011 at 09:35 UTC | |
by jethro (Monsignor) on Jul 19, 2011 at 12:33 UTC |