944,165 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 3116
  • Perl RSS
Oct 26th, 2009
0

search an array for strings

Expand Post »
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
perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2. %fruit=(banana=>1,apple=>2,orange=>5);
  3. $target = %fruit;
  4.  
  5. open(INPUT, "<fruits.txt");
  6.  
  7. while (<INPUT>) {
  8. if (/$target/) {
  9. print "Found $target on line $.";
  10. }
  11. }
  12. close(INPUT);
I have attached the file which i am to search .
Attached Files
File Type: txt fruits.txt (327 Bytes, 52 views)
Similar Threads
Reputation Points: 11
Solved Threads: 7
Junior Poster
notuserfriendly is offline Offline
102 posts
since Jan 2007
Oct 26th, 2009
1
Re: search an array for strings
Perl Syntax (Toggle Plain Text)
  1. %fruit=(banana=>1,apple=>2,orange=>5);
  2. #$target = %fruit;
  3. #why u have used above line?what it means?in perl it is of no use.
  4.  
  5.  
  6.  
  7. open(INPUT, "<fruits.txt");
  8.  
  9. while (<INPUT>)
  10. {
  11. foreach $target(keys %fruit)
  12. {
  13. if (/$target/)
  14. {
  15. print "Found $target on line $.\n";
  16. }
  17. }
  18. }
  19. close(INPUT);
Last edited by vbharathi; Oct 26th, 2009 at 5:35 am.
Reputation Points: 13
Solved Threads: 1
Newbie Poster
vbharathi is offline Offline
8 posts
since Oct 2009
Oct 26th, 2009
0
Re: search an array for strings
Hey thank you very much, i am new to perl and as you can see im trying to learn on my own. Been reading some online tutorials and so far i like perl but still a novice.
Reputation Points: 11
Solved Threads: 7
Junior Poster
notuserfriendly is offline Offline
102 posts
since Jan 2007
Oct 26th, 2009
0
Re: search an array for strings
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)
  1. $target =~ m/you/;
Reputation Points: 11
Solved Threads: 7
Junior Poster
notuserfriendly is offline Offline
102 posts
since Jan 2007
Oct 26th, 2009
0
Re: search an array for strings
$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.
Reputation Points: 11
Solved Threads: 5
Junior Poster in Training
onaclov2000 is offline Offline
57 posts
since Jun 2008
Oct 26th, 2009
0
Re: search an array for strings
So basically what you are saying onaclov2000 is that the only way to delete a line is to write the whole thing in a different file omitting the line you do not want that begins with the specified string?
Reputation Points: 11
Solved Threads: 7
Junior Poster
notuserfriendly is offline Offline
102 posts
since Jan 2007
Oct 29th, 2009
0
Re: search an array for strings
So basically what you are saying onaclov2000 is that the only way to delete a line is to write the whole thing in a different file omitting the line you do not want that begins with the specified string?
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.
Reputation Points: 153
Solved Threads: 142
Master Poster
d5e5 is offline Offline
740 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Parsing of information in perl
Next Thread in Perl Forum Timeline: CPAN Perl module Installation Problem on Mac





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC