| | |
search an array for strings
![]() |
•
•
Join Date: Jan 2007
Posts: 74
Reputation:
Solved Threads: 5
basically i am trying to search for similar strings in an array which is hardcoded into the script and compare them to and find the line they exist on the text file
here is my code so far
I have attached the file which i am to search .
here is my code so far
perl Syntax (Toggle Plain Text)
#!/usr/bin/perl %fruit=(banana=>1,apple=>2,orange=>5); $target = %fruit; open(INPUT, "<fruits.txt"); while (<INPUT>) { if (/$target/) { print "Found $target on line $."; } } close(INPUT);
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 1
1
#2 32 Days Ago
Perl Syntax (Toggle Plain Text)
%fruit=(banana=>1,apple=>2,orange=>5); #$target = %fruit; #why u have used above line?what it means?in perl it is of no use. open(INPUT, "<fruits.txt"); while (<INPUT>) { foreach $target(keys %fruit) { if (/$target/) { print "Found $target on line $.\n"; } } } close(INPUT);
Last edited by vbharathi; 32 Days Ago at 5:35 am.
•
•
Join Date: Jan 2007
Posts: 74
Reputation:
Solved Threads: 5
0
#4 32 Days Ago
Hey that worked but i have another question and i dont want to start another thread ( will if i have to). How would i be able to delete a line that started with a certain string, for example in this file "you". Is this easy to approach? Is there a delete function used with
perl Syntax (Toggle Plain Text)
$target =~ m/you/;
•
•
Join Date: Jun 2008
Posts: 49
Reputation:
Solved Threads: 5
0
#5 32 Days Ago
$target =~ m/^[yY]ou/; # this will grab lines starting with you or You (not sure if the character class is right, but it looks to be).
just do an "if" on that above search, and then don't print if it contains it, now if it's a multiline situation, you'll have to do some more adjusting on what lines to not print.
just do an "if" on that above search, and then don't print if it contains it, now if it's a multiline situation, you'll have to do some more adjusting on what lines to not print.
My Blog:
Onablog
Onablog
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
0
#7 29 Days Ago
Pardon my jumping in. I'm new to Perl but I have read there is such a thing as doing an in place edit on a file, which I think is what you are asking about. If you Google perl "in place edit" $^I you will see a lot has been written about it.
![]() |
Similar Threads
- Search an array of objects. (C++)
- Declaring an array of strings in C++ (C++)
- Initializing an array of strings and printing it. (C++)
- Binary Search of an array of three strings (C)
- Sorting a 2d array of strings in C++?? (C++)
- Is there a way to tokenize an array of strings (C++)
- filling an array with strings using gets() (C)
Other Threads in the Perl Forum
- Previous Thread: Parsing of information in perl
- Next Thread: CPAN Perl module Installation Problem on Mac
| Thread Tools | Search this Thread |
adsense adult advanced advertising apple array art asp assembly bing blogging book business c# c++ char character consumersearch convert copyright count cuil data database delete development dynamic earth engine enterprisesearch europe facebook file format frequency function google graph hacking image index int internet internetadvertising iphone java javascript legal list listbox malware marketing math mcafee memory menu microsoft monetization msn multiple mysql news number optimization parse pdf perl php pointer privacy program publishing python regex revenue rss search searchengine security seo sex socialnetworking software space spyware string table twitter universe upload vb videosearch web webmaster website wiki wolfram wolframalpha yahoo yahoo!






