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
|