Solved

Max word filter bugs

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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