Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 28th, 2008, 10:57 PM
Gojippo Gojippo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Gojippo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 58 sec
Reputation Power: 0
Subroutine to check word presence

Hello there, I'm quite new to using Perl and I still have a lot to learn. Would be nice if you could help me on that one.

I handle dictionary data and I have to write a script that checks if words in data corpus exists or not in a word-list file. If it exists, it has to do nothing but if it doesn't, I want it to print that word.

The point is, I have to NOT use hashes or grep to look for the presence. So here comes subroutine (or so I think, but if you have better advice, I'm all ears).

So first, I open the word-list file (one word per line format), I push each word in an array and close the file. Then I use sort to sort alphabetically the array.

I then open the corpus data (also one word per line), and it's there that I'm stuck. If using subroutine, I know I have to use cmp to do a binary search, but I don't know at all how you can write it to compare each readen word of the corpus data with the word-list data. How should it be written ?

Any help is welcome

Reply With Quote
  #2  
Old April 28th, 2008, 11:55 PM
KevinADC's Avatar
KevinADC KevinADC is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2004
Location: Sunny Southern California
Posts: 1,966 KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 3 h 4 m 48 sec
Reputation Power: 338
Why the restrictions: no hash or grep? Is this school work?

Reply With Quote
  #3  
Old April 29th, 2008, 12:23 AM
Gojippo Gojippo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Gojippo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by KevinADC
Why the restrictions: no hash or grep? Is this school work?


I'm studying perl at work, and already having learnt about hashes and arrays, the guy teaching me perl wants me to do it with something else (subroutines ?).

Reply With Quote
  #4  
Old April 29th, 2008, 01:13 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
If you are using perl 5.10, you can use smart matching, which is really cool. Otherwise, you can use a foreach loop on the array for every line of the file, in consideration of the restrictions placed on you.

Reply With Quote
  #5  
Old April 29th, 2008, 04:29 AM
Gojippo Gojippo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Gojippo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by keath
If you are using perl 5.10, you can use smart matching, which is really cool.


Thank you for your help. First time I heard about smart matching. I googled it and it seems quite effective.

Quote:
Originally Posted by keath
Otherwise, you can use a foreach loop on the array for every line of the file, in consideration of the restrictions placed on you.


Ok, some questions about this. Wouldn't you then need to use grep on this ? I'm still learning so I may not understand really how you mean this. Another problem is that the file is quite huge (~100000 lines) so it would take a lot of time with foreach, or do I understand this wrong ?

Anyway, thanks a lot for your help.

Reply With Quote
  #6  
Old April 29th, 2008, 04:48 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
I thought one of the constraints was that you couldn't use grep.

I would rather use a hash, but I was thinking along the lines of
Code:
while (<>) {
   chomp;
   foreach my $word (@word_list) {
      print "$_\n" if $word eq $_;
   }
}

Reply With Quote
  #7  
Old April 29th, 2008, 04:54 AM
Gojippo Gojippo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Gojippo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by keath
I thought one of the constraints was that you couldn't use grep.

I would rather use a hash, but I was thinking along the lines of
Code:
while (<>) {
   chomp;
   foreach my $word (@word_list) {
      print "$_\n" if $word eq $_;
   }
}


Well, I can't use grep, but your code seems good too. Thank you, I'll give it a try !

Reply With Quote
  #8  
Old April 29th, 2008, 08:52 PM
Gojippo Gojippo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Gojippo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 58 sec
Reputation Power: 0
Ok, back I am. It seems like the perl version we use at work is outdated, so no sweet smart-matching for me.

Also, keath, (tell me if I'm wrong, but) your code prints only if the read word EXISTS in the array, but I have to print only what doesn't exist in the array. Without hashes and grep, I'm such a sucker

Any help ?

Reply With Quote
  #9  
Old April 29th, 2008, 09:22 PM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
Quote:
Originally Posted by Gojippo
Any help ?

Check the whole array first before drawing a conclusion and printing the word. Create a flag variable. Set it in the loop if there's a match, then check at the end and see if the flag was set. If it wasn't set, print.

But I have to say that the exercise of trying to solve a problem using perl, but not allowing one to use the features of that language, seems pointless. I would rather someone taught me the language, rather than teach me how to use the language in an imaginary world where it didn't have certain features.

Reply With Quote
  #10  
Old April 29th, 2008, 09:29 PM
Gojippo Gojippo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Gojippo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 58 sec
Reputation Power: 0
Thanks for your help, keath.

Quote:
But I have to say that the exercise of trying to solve a problem using perl, but not allowing one to use the features of that language, seems pointless. I would rather someone taught me the language, rather than teach me how to use the language in an imaginary world where it didn't have certain features.


So true...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Subroutine to check word presence


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway