Solved

Max word filter bugs

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
Six Sigma Control Plans

717 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