[tpm] Split a string in half

Abram Hindle abram.hindle at softwareprocess.us
Sat Mar 14 21:07:54 PDT 2009


I misread what you wrote (I read too much into the examples).

I've attached my embarrassing split on space code at the bottom which
just finds the split space point w/o the assumption of a middle space.

abram

Madison Kelly wrote:
> Madison Kelly wrote:
>> Hi all,
>>
>>   I need to split a sting in half, and I can assume the the middle
>> character in the string will be a space ' ' character. I cannot
>> predict how many spaces will be in the string though.
>>
>>   In short; How could I split:
>>
>> 'foo bar foo bar' => 'foo bar', 'foo bar'
>> 'baz baz'         => 'baz', baz'
>> 'foo bar baz foo bar baz' => 'foo bar baz', 'foo bar baz'
>>
>>   In Detail;
>>
>>   I use WWW::Mechanize, and I am running into a problem with a website
>> that uses an image and text in a single anchor. Specifically, the alt
>> text is the same as the text, so the 'find_all_links' function returns:
>>
>> foo bar foo bar
>>
>>   For:
>>
>> <a href="..."><img src="..." alt="foo bar"> foo bar</a>
>>
>>   I never know what the link will be, only that the alt and text will
>> be that same.
>>
>> Thanks!
>>
>> Madi
> 
> For the record, I can do this now, but it's ugly as sin. Surely a TPM'er
> will have a more elegant/efficient method.
> 
> :)
> 
> My ugly way (where 'say_name' is the string in question):
> 
> -----------------------------------------
> if (length($say_name)%2)
> {
>     my $half_count=(length($say_name)-1)/2;
>     my $half_string="";
>     my $i=0;
>     foreach my $char (split//, $say_name)
>     {
>         $half_string.=$char;
>         $i++;
>         last if $i >= $half_count;
>     }
>     $say_name=$half_string if $say_name eq "$half_string $half_string";
> }
> -----------------------------------------
> 
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm



# Placed under WTFPL V2
# See copyright notice at bottom (it is NSFW)

# midpointspace($str)
# string -> int
# -1 indicates no space found to split on
# finds the position in the string of the space you might want to split on
# the space found should be the middle most space
# the function doesn't recognize anyother whitespce
sub midpointspace {
  my ($str) = @_;
  #position to index from
  my $pos = 0;
  #our current midpoint
  my $mid = -1;

  my $i = 0;
  #this biases it to the front
  #remove int to avoid it
  my $best_mid = int(length($str)/2);

  while(-1 != ($i = index($str," ",$pos))) {
    my $dist      = abs($best_mid - $i);
    my $best_dist = abs($best_mid - $mid);
    if ($dist < $best_dist) {
      $mid = $i;
    }
    $pos = $i+1;
  }
  return $mid;
}

sub test_midpointspace {
  assert(0,    midpointspace(" "),"1 space");
  assert(-1,   midpointspace(""),"Empty");
  assert(0,    midpointspace(" "),"1 space");
  assert(-1,    midpointspace("a"),"1 char");
  assert(1,    midpointspace("a b"),"middle");
  assert(0,    midpointspace(" ab"),"start");
  assert(2,    midpointspace("ab "),"end");
  assert(3,midpointspace("aaa b"),"aaa b");
  assert(3,midpointspace("aaa bbb"),"aaa bbb");
  assert(7,midpointspace("aaa bbb aaa b"),"aaa bbb aaa b");
  assert(3,midpointspace("aaa bbb aaa"),"aaa bbb aaa");

}
sub assert {
  die "$_[2] [$_[0]] [$_[1]]" unless ($_[0] == $_[1]);
}
test_midpointspace();

__DATA__
            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                    Version 2, December 2004

 Copyright (C) 2009 Abram Hindle

 Everyone is permitted to copy and distribute verbatim or modified
 copies of this license document, and changing it is allowed as long
 as the name is changed.

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. You just DO WHAT THE FUCK YOU WANT TO.

 This program is free software. It comes without any warranty, to
 the extent permitted by applicable law. You can redistribute it
 and/or modify it under the terms of the Do What The Fuck You Want
 To Public License, Version 2, as published by Sam Hocevar. See
 http://sam.zoy.org/wtfpl/COPYING for more details.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20090315/39aa37ec/attachment.bin>


More information about the toronto-pm mailing list