Solved

Max word filter bugs

Posted on 2003-10-28
3
167 Views
Last Modified: 2010-05-18
Hello

i have a code belows;
my $max_word = 22;
my $advertbody = join (" ", (split (/\s/, $advertbody))[0..$max_word-1]);
print "advertbody limited--> ", $advertbody, "\n";

The code accept up to 22 words.
If there are for example, 10 words comes it prints the rest of 12 words as empty spaces.
It looks like this; abc aksd a                               |as|1233|as| and so on.
This space is wrong, how can i change in code?                            
0
Comment
Question by:tilmes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9637155
You could try putting in an intermediate step where you can reset the max value if you don't have as many elements as the max. For example:


#!perl -w

use strict;



my $max_word = 22;

my $string = "a b c d e f g";

my @temp_results = split /\s+/, $string;


$max_word = #@temp_results unless (#@temp_results > $max_word);


print (join ' ', @temp_results[0..$max_word-1]);
0
 

Author Comment

by:tilmes
ID: 9637253
Hello,

i am sorry.
I can not start with that code.
Could you make it with scalar $advertbody
and print "$advertbody" ?
Is it possible if the words ecceed over 30, then don't accept more than that?
The script becomes datas from another script regularly.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 50 total points
ID: 9638369
>> I can not start with that code.

I'm not sure which part of ultimatemike's code you're refering to, but will this do what you what?

use strict;

my $max_word = 7;

my $advertbody = "This code will accept truncate a string of words at position n";

my @advert = split /\s+/, $advertbody;

$advertbody = join ' ', splice(@advert, 0, $max_word);

print $advertbody;

----outputs---
This code will accept truncate a string
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question