Google Answers Logo
View Question
 
Q: Simple modification in PERL ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: Simple modification in PERL
Category: Computers > Programming
Asked by: questioner1234-ga
List Price: $3.50
Posted: 05 Aug 2003 10:48 PDT
Expires: 04 Sep 2003 10:48 PDT
Question ID: 240324
I need to modify this perl script. 

# create file
$NEWFILE = "/usr/www/titles/$ProductName.shtml";
$shtml = qq[<!--#include
virtual="/cgi-bin/products.cgi?input_item=$Asin&input_search_type=AsinSearch"
-->];
$shtml .= qq[<!-- $ProductName -->];

if ($NEWFILE ne '') {
open (NEWFILE, ">$NEWFILE");
print NEWFILE "$shtml";
close (NEWFILE);
}
# end create file

So right now the name of the file created is the variable
$ProductName. I would like to create a new variable
$TruncatedProductName. I would like that variable to be the same as
$ProductName, but with the spaces converted to underscore (_) and
truncated either

1. before the first colon
2. or if there is no colon, after 25 characters, truncate at the end
of a word.

So, if the $ProductName is "Go to the store and buy some milk and eat
it"
then $TruncatedProductName should be "Go_to_the_store_and_buy" 

If it is "Go to the store: buy some milk" then it should be
"Go_to_the_store"

Clarification of Question by questioner1234-ga on 05 Aug 2003 10:49 PDT
Also if possible, I would like to remove punctuation marks from the string.
Answer  
Subject: Re: Simple modification in PERL
Answered By: webadept-ga on 05 Aug 2003 11:12 PDT
Rated:4 out of 5 stars
 
Hi, 

#! perl


$ProductName = "Go. to the! store?: buy some milk!";

# Use Split to break out the truncated part from the rest of the 
# sentence 
my ($TruncatedProductName, $restOfName) = split /:/, $ProductName; 

# Now we use regex to do the rest

# remove punctuation marks from the string 

 $TruncatedProductName =~ s/\!|\?|\.//sig;

# change spaces to underscores 

 $TruncatedProductName =~ s/ /_/sig;

# and see what we have now 
print $TruncatedProductName, "\n";


And there you have it, no worries at all. 

Thanks, 

webadept-ga

Request for Answer Clarification by questioner1234-ga on 05 Aug 2003 11:18 PDT
seems to work well but I wanted to limit the number of characters to
25. Also I think I should remove "the" "a" "and" "of" "on" "or".

Request for Answer Clarification by questioner1234-ga on 05 Aug 2003 11:19 PDT
Also I will add $2 in payment to add the removal of "the" "a" "and"
"of" "on" "or". thanks

Clarification of Answer by webadept-ga on 05 Aug 2003 16:44 PDT
Hi, 

In the future when dealing with the Researchers it is better to wait
until they have responded to all of your clarification requests before
rating the question.

Here is the rest of the code you are looking for:

#! perl 
 
 
$ProductName = "Go. to the! store?: buy some milk!"; 

# check to see if it is more than 25 characters in length
my $l = length($ProductName);
if($l > 25)
  {
  print "Greater", "\n";
    if(index($ProductName,":") > 25)
       {
         print "Truncating \n"; 
         $TruncatedProductName = substr($ProductName,0,25);
	 print $TruncatedProductName, "\n";
       }
    else
       {


# Use Split to break out the truncated part from the rest of the  
# sentence  
	 ($TruncatedProductName, $restOfName) = split /:/, $ProductName;  
	 print $TruncatedProductName, "\n";
       } # end if else for index of : 
       
 } # end check for length 
# Now we use regex to do the rest 
 
# remove punctuation marks from the string  
 
 $TruncatedProductName =~ s/\!|\?|\.//sig; 
 
	 print $TruncatedProductName, "\n";
# remove "the" "a" "and" "of" "on" "or"

 $TruncatedProductName =~ s/( the | a | and | of | or )/ /sig;
	 print $TruncatedProductName, " : here\n"; 

 #remove extra spaces created by taking out the words above 
 while($TruncatedProductName =~ /  /)
  {
    $TruncatedProductName =~ s/  / /sig; 
  }

# change remaining spaces to underscores  
 
 $TruncatedProductName =~ s/ /_/sig; 
 
# and see what we have now  
print $TruncatedProductName, "\n"; 

Thanks again, 

webadept-ga

Clarification of Answer by webadept-ga on 05 Aug 2003 16:46 PDT
oh darn.. forgot something there.. the truncating after last word..
I'll get that to you in a bit.. sorry..

webadept-ga

Clarification of Answer by webadept-ga on 05 Aug 2003 16:59 PDT
#! perl 
$ProductName = "Go. to the! store? buy some milk!: and some cookies"; 

# check to see if it is more than 25 characters in length
my $l = length($ProductName);
if($l > 25)
  {
  print "Greater", "\n";
    if(index($ProductName,":") > 25)
       {
         print "Truncating \n"; 
	 
	 @mystring = split/ /, $ProductName;
	 my $i = 0;
	 my $c = 0;

	 while($c < 25)
	  {
	    $c = $c + length($mystring[$i]);
	    
	    if($c < 25)
	       {
                  $TruncatedProductName =  $TruncatedProductName." ".$mystring[$i];
	       }
	    $i++;   
          } # end while 
           $TruncatedProductName =~ s/^\s+//;  # remove the leading white space

	 
       }
    else
       {


# Use Split to break out the truncated part from the rest of the  
# sentence  
	 ($TruncatedProductName, $restOfName) = split /:/, $ProductName;  
	 print $TruncatedProductName, "\n";
       } # end if else for index of : 
       
 } # end check for length 
# Now we use regex to do the rest 
 
# remove punctuation marks from the string  
 
 $TruncatedProductName =~ s/\!|\?|\.//sig; 
 
	 print $TruncatedProductName, "\n";
# remove "the" "a" "and" "of" "on" "or"

 $TruncatedProductName =~ s/( the | a | and | of | or )/ /sig;
	 print $TruncatedProductName, " : here\n"; 
 while($TruncatedProductName =~ /  /)
  {
    $TruncatedProductName =~ s/  / /sig; 
  }

# change spaces to underscores  
 
 $TruncatedProductName =~ s/ /_/sig; 
 
# and see what we have now  
print $TruncatedProductName, "\n"; 


That should do it for you. 

Thanks and sorry for the oversites

webadept-ga

Request for Answer Clarification by questioner1234-ga on 05 Aug 2003 18:20 PDT
Replacing the "the" doesn't work if there's more than one "the" or if
it is "of the" it seems to only replace the "the" not the "of"

So:

The New Drawing on the Right Side of the Brain

comes out:

The_New_Drawing_on_Right_Side_the_Brain

Clarification of Answer by webadept-ga on 05 Aug 2003 23:29 PDT
Hi, 

I also changed the word removal around, to take out the unwanted
words, before the truncating begins, giving your final outcome a
better chance of being meaningful..

#! perl  
my $ProductName = "The New Drawing on the Right Side of the Brain ";

my $i = 0;
my $c = 0;
my $TruncatedProductName = "";
my $l = 0; 
my @mystring =();

# clean  the string 
#
   
$ProductName =~ s/^\s+//;

$ProductName =~ s/\s+$//;



 while($ProductName =~ /  /) 
    { 
       $ProductName =~ s/  / /sig;  
    } 

while(  $ProductName =~ / the |^the /)
  {
     $ProductName =~ s/ the |^the / /sig;
  }

while(  $ProductName =~ / a |^a /)
  {
     $ProductName =~ s/ a |^a / /sig;
  }
while(  $ProductName =~ / and |^and /)
  {
     $ProductName =~ s/ and |^and / /sig;
  }
while(  $ProductName =~ / or |^or /)
  {
     $ProductName =~ s/ or |^or / /sig;
  }
while(  $ProductName =~ / of |^of /)
  {
     $ProductName =~ s/ of |^of / /sig;
  }

  
 
# check to see if it is more than 25 characters in length 
 $l = length($ProductName); 
if($l > 25) 
  { 
    if(index($ProductName,":") > 25) 
       { 
   
  	my @mystring = split/ /, $ProductName; 
  	my $i = 0; 
  	my $c = 0; 
 
  	while($c < 25) 
   	{ 
     		$c = $c + length($mystring[$i]); 
      
     	    if($c < 25) 
        	{ 
                  $TruncatedProductName =  $TruncatedProductName."
".$mystring[$i];
        	} 
     	    $i++;    
        } # end while  
      $TruncatedProductName =~ s/^\s+//;  # remove the leading white
space
 
   
       } 
    else 
       { 
   	         @mystring = split/ /, $ProductName; 
  		 $i = 0;
  		 $c = 0; 
 
  		while($c < 25) 
   		{ 
     			$c = $c + length($mystring[$i])+1; 
      
     			if($c < 25) 
        		{
                  	  $TruncatedProductName =  $TruncatedProductName."
".$mystring[$i];
        		}# end if truncating  
     			$i++;    
          	} # end while  
		
      	$TruncatedProductName =~ s/^\s+//;  # remove the leading white
space
	
 
          # Use Split to break out the truncated part from the rest of
the
          # sentence 
	  
	  

       } # end if else for index of :  
        
 } # end check for length 
else
 {
              ($TruncatedProductName, $restOfName) = split /:/,
$ProductName;
 }
 
# Now we use regex to do the rest  
  
# remove punctuation marks from the string   
  
 $TruncatedProductName =~ s/\!|\?|\.//sig;  
  
  
  while($TruncatedProductName =~ /  /) 
    { 
       $TruncatedProductName =~ s/  / /sig;  
    } 
$TruncatedProductName =~ s/^\s+//;

$TruncatedProductName =~ s/\s+$//; 
# change spaces to underscores   
  
 $TruncatedProductName =~ s/ /_/sig;  
  
# and see what we have now   
print $TruncatedProductName, "\n";  



Thanks, 

webadept-ga

Clarification of Answer by webadept-ga on 05 Aug 2003 23:46 PDT
Also, one of the great things about Perl is there is always more than
one way to do something. Instead of all those while loops for each
word, we could make it a single while loop, putting all the words you
want to take out in an array.

This doesn't make it any faster, but it is easier to edit this way,
when you want to add a word, just add it to the array.

# clean  the string 
#
   


my @myXwords = ("( T|the )","( A|a )","( A|and )", "( O|or )", "( O|of
)");

foreach my $xword (@myXwords)
 {

   while($ProductName =~ /(^| )$xword/sig)
     {
	 $ProductName =~ s/(^| )$xword/ /sig;
     }
 }

$ProductName =~ s/^\s+//;

$ProductName =~ s/\s+$//;
 
 while($ProductName =~ /  /) 
    { 
       $ProductName =~ s/  / /sig;  
    } 


thanks, 

webadept-ga
questioner1234-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy