Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logical Or ? 2

Status
Not open for further replies.

goliath

MIS
Aug 18, 1999
62
US

Greetings,

I'm struggling with a little perl quandry.

I'm reading in a file to parse but some of the values I read in have different words for the same value, so I would like the related words to be converted to the common name.

For example, if I read in "large" or "Huge" or "Monsterous" I want to change the value to "BIG"

Similar for small and medium with similar related verbage.

so I tried to find an easier way than a huge IF then Else statement...

Any suggestions? I'm trying to figure out if I can use a hash effeciently. I'm parsing a 29MB file so I'm trying to keep it clean.

Thanks!
 
You could go
Code:
$var =~ s/large|huge|monstrous/big/ig;
 
Code:
@BIGwords = qw(large huge monstrous and_so_on);

while(<FILE>){
  foreach $word(@BIGwords){
    s/$word/\bBig\b/g;
    }
  }

You could do something like that, and even do an array of arrays for many different types of word replacements. If you nested correctly, you would only iterate over the file contents a single time, checking for all possible matches in your 2D array.

--jim
 
I'm not doing a good job of describing my scenerio, but:

- Class can be one of 14 title types
- Each Class has 4 synonymous words (sub-titles) of which I want to resolve to the one word per class

It seems to me that a hash is my best option? I have yet to build a hash so this is a challenge for me.
 
Yeah, a hash is probably your best option.

Try this:

Code:
%Classes = (
  'big'   => [ qw(giant large huge monstrous) ],
  'small' => [ qw(tiny minute minimal speck) ],
  ...etc...
);
open(YOURFILE,&quot;<path/to/your/file&quot;);
while(defined($Line = <YOURFILE>)) {
  foreach $Key (keys %Classes) {
    for $Subtitle (0..3) {
      $SearchFor = $Classes{$Key}[$Subtitle];
      $Replace   = $Key;
      $Line =~ s/$SearchFor/$Replace/ig;
    }
  }
  # Do what you want with $Line here.
}
close YOURFILE;
 
This is really good, I'm close I think...

What do I do if one of the 3 possible words is a double word?

I've tried single and double quotes - neither works right.

example:
'small' => [ qw(tiny minute minimal 'really small') ],
 
small => [&quot;tiny&quot;,&quot;minute&quot;,&quot;minimal&quot;,&quot;really small&quot;],

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top