Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 22nd, 2006, 10:22 AM
Registered User
 
Join Date: Nov 2005
Location: , , .
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));
close(DIR);

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

@sortf = sort(@folders);

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

$cnt=@folders;
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

}

close(FILE);
#-------------------------------------------------------

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

Thanks in advance.





Reply With Quote
  #2 (permalink)  
Old March 6th, 2006, 05:36 PM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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";

HTH
Charlie

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
first element in array nesting siblings rjonk XSLT 1 May 18th, 2008 04:53 PM
check if an array element is not undefined crmpicco Javascript How-To 1 October 6th, 2005 12:40 PM
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 04:34 AM
Removing an Element from an Associative Array nick8245 Javascript 2 September 26th, 2003 01:15 PM



All times are GMT -4. The time now is 05:39 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.