From: Mr P on
On Jul 15, 9:43 am, "Dr.Ruud" <rvtol+use...(a)xs4all.nl> wrote:
> Mr P wrote:
> > On Jul 14, 2:32 pm, Jozxyqk <jfeue...(a)eecs.tufts.edu> wrote:
> >> Mr P <misterp...(a)gmail.com> wrote:
> >>> Lets say I want the CENTER 2 characters from any (even length) string?
> >>> is there A regex that can handle this for ANY even length string?  Im
> >>> pretty good with regexes but I cant think of one that can handle it.
>
> >> Why use a regex?
>
> >> substr($string,length($string)/2-1,2);
>
> > I'd thought of this approach and even played with it a bit- you left
> > out the test for *evenness* however. I just love regexes I'd hoped
> > there was something simple Id missed like some assertion that would
> > make it EZ.. Ben's solution is pretty durn good though!
>
> Question 1 was: Why use a regex?
> Question 2 is: Why did you lie about the string having an even length?
>
> And as a direction, you presented a substitution in stead of a regex.
>
> perl -wle '
>      my $center = @ARGV ? $ARGV[0] : q{TE$T};
>      exit 1 if $center =~ /\A(?:..)*.\z/s;  # odd
>
>      1 while $center =~ s/\A.(.{2,}).\z/$1/s;
>
>      print $center;
> '
> E$
>
> --
> Ruud

OMG dude!
From: Uri Guttman on
>>>>> "P" == P <misterperl(a)gmail.com> writes:

P> Just to see what error it would throw- I tried this:
P> s/^(.*)(..)(.*)$/$2/ if length $1 == length $2;

P> Curiously it said *attempted to modified a read-only value* which I
P> thought was odd- I don't see where I'm trying to do that at all. I
P> expected it to say something was un-innited or something like that?

regardless of that error, that will never work. the if modifier is
executed BEFORE the s///. that means it looks at the previous settings
of $1 and $2 from some other regex.

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: C.DeRykus on
On Jul 15, 9:31 am, "Dr.Ruud" <rvtol+use...(a)xs4all.nl> wrote:
> Justin C wrote:
> > my $c = (length($_) / 2) - 1;
> > $_ =~ s/^.{$c}(.{2}).{$c}$/$1/;
>
> You can get into trouble there with embedded newlines.
>
> You can change it to
>
>    ( $_ ) = /^ .{$c} (..) /sx;

or, even prime it with pos(), although
maybe there'd be no speedup.

pos() = $c;
($_) = /(..)/g;

>
> Or even better, to
>
>    $_ = substr( $_, length() / 2 - 1, 2 );
>

Agree totally - this'd be fastest.

--
Charles DeRykus
From: Hecht on
On 14.07.2010 20:22, Mr P wrote:
> Lets say I want the CENTER 2 characters from any (even length) string?
>
> 123456 .. 34
> ABC123 .. C1
> 1223 ..22
> 1234567890 .. 56
> AB .. AB
>
>
> is there A regex that can handle this for ANY even length string? Im
> pretty good with regexes but I cant think of one that can handle it.
>
> sorta like
> s/(.+)(..)(.+)/$2/ where the length of $1 == length of $2..
>
> Thanks!
I would do this in this way:


my $string = "123456";
my @string = split //, $string;
my $length = @string;
my $result= $length/2;
print $string[$result-1] . $string[$result];

crude but works.

Daniel
From: Willem on
Hecht wrote:
) I would do this in this way:
)
)
) my $string = "123456";
) my @string = split //, $string;
) my $length = @string;
) my $result= $length/2;
) print $string[$result-1] . $string[$result];
)
) crude but works.

Very crude indeed, given that the same can be achieved with simple
string operations, such as 'length' and 'substr', as already shown
crossthread. substr($string,length($string)/2-1,2) does the trick.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT