Solved

Max word filter bugs

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

895 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

14 Experts available now in Live!

Get 1:1 Help Now