Solved

Max word filter bugs

Posted on 2003-10-28
3
163 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video discusses moving either the default database or any database to a new volume.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now