PDA

View Full Version : taking the first, say, 200 characters


sarah_anne
02-15-2003, 10:51 AM
Hello -

I'm not sure where to even begin to do this, as I've not come across it anywhere in my search for Perl tips and tutorials, so....


I have a variable amount of text, named $text, can be anything from one character to 2000. Now how would I be able to print the first 200 characters of this data? Or, even better, since the data is text in sentance form, the first two sentances (seperated by full stops) and then half of the next one?

I saw this at http://www.doctorjob.com/forum and it seemed like a nice intro into what a post was about, you see.


Any help would be great, as always :D

Sarah.

Grizz2
02-16-2003, 05:18 AM
Well I didn't test it and you might have to play around a bit but something like this might work,

@newstring = "";
for($i=0; $i < (length($text)+ 1); $i++)
{
$x = substr($text,$i,1);
if($i > 200 && $x ~= /\s/)
{
last;
}else{
push(@newstring, $x);
}
}
print @newstring;

Grizz2
02-16-2003, 05:26 AM
I should have explained that I guess. The previous code should break $text into a string at the first space, line break, return or tab, after it reaches 200.
If you just want the first 200 characters this will do it.

$newstring = substr($text, 0, 200);
print $newstring;