Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 22nd, 2006, 10:22 AM
Registered User
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default search a file for every element in array and appen

Hi All,

Can you please tell me how to search in a txt file for each array element.

If my array contains 3 elements say io, new folder,po,new folder(2).
i want to check these elements in a text file one by one if it exists then exit the loop if it does not exist then append the elements to the text file.

i tried the below code but i am certain only the first half is correct and requires some help for the second half

chdir('D:\pp') or die "$!";
opendir(DIR,'.') or die "$!";

@folders = grep (-d && !/^\.+$/, readdir(DIR));

print "before sorting\n";
print "$_\n" for @folders;

@sortf = sort(@folders);

print "after sorting\n";
print "$_\n" for @sortf;

print $cnt;

open(FILE,"+>>DB.txt") or die "$!";

while ( <FILE> )
 $myfile = $myfile . $_;


foreach my $dirs (@sortf) {
 @times= grep(/$dirs/,$myfile);

#i am not sure how to append the non existing entries to the file with the output of grep



Can any one let me know the right way of doing this.

Thanks in advance.

Old March 6th, 2006, 05:36 PM
Friend of Wrox
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

To open a text file and check for every element in an array within the file:

 - open the file
 - read it into an array
 - close the file
 - for each element of the file_array, grep for each element of the targets_array

You'll need to understand map and grep to get this:

#!/usr/bin/perl -w
# look for elements of array @targets in file $file
use strict;

my @targets=qw|target1 target2 target3|;
my $file = 'myfile';

open (INPUT, $file);
my @file_lines = <INPUT>;
close INPUT;

my @found_lines = map { my $tg=$_;
                        grep {/$tg/x} @targets } @file_lines ;

print join ", ",@found_lines;
print "\n";


Don't Stand on your head - you'll get footprints in your hair

Similar Threads
Thread Thread Starter Forum Replies Last Post
first element in array nesting siblings rjonk XSLT 1 May 18th, 2008 03:53 PM
check if an array element is not undefined crmpicco Javascript How-To 1 October 6th, 2005 11:40 AM
Array Element android66 Javascript 2 November 12th, 2004 04:50 AM
How do I test for an empty array element kmoran Excel VBA 1 October 8th, 2004 03:34 AM
Removing an Element from an Associative Array nick8245 Javascript 2 September 26th, 2003 12:15 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.