Re: Limiting String Length

2001-06-26 Thread M. Buchanan

There is a length() function.  

eg. $string = 'my little little string';
$lengthOfstring = length($string);

if ( $lengthOfstring  >= 4096 blah)  {
blah
}

- Original Message - 
From: "Chuck Ivy" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, June 21, 2001 2:31 PM
Subject: Limiting String Length


> I've got a simple message board script that I cobbled together. It seems 
> to have been the target of some abuse, though.
> 
> The latest patch I'd like to apply to the code would be limiting the 
> length of a message posted to, say, 4096 characters.
> 
> Now, looking up the substring function, it looks like if the original 
> string were less than the size of my substring, it would pad my variable 
> until it was 4096 characters.
> 
> Would a regex be better? Matching for up to 4096 characters and 
> replacing the string with $1?
> 
> I recall some programming languages treat strings as arrays of 
> characters. Is there a quick perl function or variable that represents 
> the length of a string? I didn't see any obvious entries in the index of 
> Programming Perl, but I may have been looking in the wrong place.
> 
> Or is this a good place for formatting commands?
> 
> Any suggestions welcome.


_
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com




Re: Limiting String Length

2001-06-21 Thread Jos I. Boumans

hahah, sure, why dont we do this then:

my $coolstring;
my @foo = split '', $str;
for (0..4095) { $coolstring .= $foo[$_] }

or how about this one:
my $foo = reverse $str;
while (length $foo > 4096) { $foo =~ s/.(.+)/$1/seg; }
$str = reverse $foo;

and no, i wont be held accountable for any loss of performance in your
script =)
TMTOWTDI ;-)

Jos

>  @str = $str =~ /(.)/;
>  $#str = 4095;
>  $str = join '', @str;
>
> Hey, TMTOWTDI, though you shouldn't overDO it =o)
>




Re: [OT] Limiting String Length

2001-06-21 Thread Paul


--- Jeff 'japhy' Pinyan <[EMAIL PROTECTED]> wrote:
> On Jun 21, Paul said:
> 
> >--- Chuck Ivy <[EMAIL PROTECTED]> wrote:
> >> I recall some programming languages treat strings as arrays of 
> >> characters. Is there a quick perl function or variable that
> >> represents 
> >> the length of a string? I didn't see any obvious entries in the
> index
> >> of 
> >> Programming Perl, but I may have been looking in the wrong place.
> >
> >I'd agree with Japhy's substring suggestion, but I think you could
> do
> >something like this if you just wanted to play array games with the
> >string:
> >
> > @str = $str =~ /(.)/;
> > $#str = 4095;
> > $str = join '', @str;
> 
> Ewww. ;)  First, you need the /s modifier on the regex.  Second, you
> need
> the /g modifier on the regex.  Third, you don't need the () in the
> regex.
> 
>   @chars = $str =~ /./gs;
> 
> Fourth, split() is far nicer to look at.
> 
>   @chars = split //, $str;
> 
> Fifth, just join() the elements you want.
> 
>   $str = join '', @chars[0 .. 4095];
> 
> Sixth, get rid of the array.
> 
>   $str = join '', (split //, $str)[0 .. 4095];
> 
> But we're not using this approach anyway, are we? ;)

LOL -- what he said. =o)


__
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/



Re: Limiting String Length

2001-06-21 Thread Jeff 'japhy' Pinyan

On Jun 21, Paul said:

>--- Chuck Ivy <[EMAIL PROTECTED]> wrote:
>> I recall some programming languages treat strings as arrays of 
>> characters. Is there a quick perl function or variable that
>> represents 
>> the length of a string? I didn't see any obvious entries in the index
>> of 
>> Programming Perl, but I may have been looking in the wrong place.
>
>I'd agree with Japhy's substring suggestion, but I think you could do
>something like this if you just wanted to play array games with the
>string:
>
> @str = $str =~ /(.)/;
> $#str = 4095;
> $str = join '', @str;

Ewww. ;)  First, you need the /s modifier on the regex.  Second, you need
the /g modifier on the regex.  Third, you don't need the () in the regex.

  @chars = $str =~ /./gs;

Fourth, split() is far nicer to look at.

  @chars = split //, $str;

Fifth, just join() the elements you want.

  $str = join '', @chars[0 .. 4095];

Sixth, get rid of the array.

  $str = join '', (split //, $str)[0 .. 4095];

But we're not using this approach anyway, are we? ;)

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
**  Manning Publications, Co, is publishing my Perl Regex book  **




Re: Limiting String Length

2001-06-21 Thread Michael Fowler

On Thu, Jun 21, 2001 at 11:31:21AM -0700, Chuck Ivy wrote:
> Now, looking up the substring function, it looks like if the original 
> string were less than the size of my substring, it would pad my variable 
> until it was 4096 characters.

> perl -wle 'foreach my $str(qw(foo foobarblahdoo))
{ print "[", substr($str, 0, 10), "]" };'
[foo]
[foobarblah]


The beauty of Perl is that you can try things very easily.  You should learn
to do this.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



Re: Limiting String Length

2001-06-21 Thread Paul


--- Chuck Ivy <[EMAIL PROTECTED]> wrote:
> I've got a simple message board script that I cobbled together. It
> seems to have been the target of some abuse, though.
> 
> The latest patch I'd like to apply to the code would be limiting the 
> length of a message posted to, say, 4096 characters.
> 
> Now, looking up the substring function, it looks like if the original
> 
> string were less than the size of my substring, it would pad my
> variable 
> until it was 4096 characters.
> 
> Would a regex be better? Matching for up to 4096 characters and 
> replacing the string with $1?
> 
> I recall some programming languages treat strings as arrays of 
> characters. Is there a quick perl function or variable that
> represents 
> the length of a string? I didn't see any obvious entries in the index
> of 
> Programming Perl, but I may have been looking in the wrong place.
> 
> Or is this a good place for formatting commands?
> 
> Any suggestions welcome.

I'd agree with Japhy's substring suggestion, but I think you could do
something like this if you just wanted to play array games with the
string:

 @str = $str =~ /(.)/;
 $#str = 4095;
 $str = join '', @str;

Hey, TMTOWTDI, though you shouldn't overDO it =o)

__
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/



Re: Limiting String Length

2001-06-21 Thread Chas Owens

On 21 Jun 2001 14:39:47 -0400, Chas Owens wrote:
> How about
> 
> use constant MAXSTRLENGTH => 4096;
> 
> substr($string, MAXSTRLENGTH - 1, -1) = '' unless(length($strlen) <
> MAXSTRLENGTH);
> 


Oops, let me ammend that to

use constant MAXSTRLENGTH => 4096;

substr($str, MAXSTRLENGTH - 1) unless (length($str) < MAXSTRLENGTH);

Sleep dep sucks.

--
Today is Boomtime, the 26th day of Confusion in the YOLD 3167
Umlaut Zebra über alles!





Re: Limiting String Length

2001-06-21 Thread Chas Owens

How about

use constant MAXSTRLENGTH => 4096;

substr($string, MAXSTRLENGTH - 1, -1) = '' unless(length($strlen) <
MAXSTRLENGTH);

On 21 Jun 2001 11:31:21 -0700, Chuck Ivy wrote:
> I've got a simple message board script that I cobbled together. It seems 
> to have been the target of some abuse, though.
> 
> The latest patch I'd like to apply to the code would be limiting the 
> length of a message posted to, say, 4096 characters.
> 
> Now, looking up the substring function, it looks like if the original 
> string were less than the size of my substring, it would pad my variable 
> until it was 4096 characters.
> 
> Would a regex be better? Matching for up to 4096 characters and 
> replacing the string with $1?
> 
> I recall some programming languages treat strings as arrays of 
> characters. Is there a quick perl function or variable that represents 
> the length of a string? I didn't see any obvious entries in the index of 
> Programming Perl, but I may have been looking in the wrong place.
> 
> Or is this a good place for formatting commands?
> 
> Any suggestions welcome.
> 
--
Today is Boomtime, the 26th day of Confusion in the YOLD 3167
Keep the Lasagna flying!





Re: Limiting String Length

2001-06-21 Thread Jeff 'japhy' Pinyan

On Jun 21, Chuck Ivy said:

>Now, looking up the substring function, it looks like if the original 
>string were less than the size of my substring, it would pad my variable 
>until it was 4096 characters.
>
>Would a regex be better? Matching for up to 4096 characters and 
>replacing the string with $1?

This is NOT the place to use a regex.  Just use the substr() function to
remove all characters after the 4096th.

  substr($entry, 4096) = "" if length($entry) > 4096;

or

  $entry = substr($entry, 0, 4096);

>I recall some programming languages treat strings as arrays of 
>characters. Is there a quick perl function or variable that represents 
>the length of a string? I didn't see any obvious entries in the index of 
>Programming Perl, but I may have been looking in the wrong place.

Perl's strings can't be manipulated as arrays at the high-end level
(that's not to say they're not implemented as char* in the source code).

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
**  Manning Publications, Co, is publishing my Perl Regex book  **