http://www.perlmonks.org?node_id=989511


in reply to Re: Retain first 4 characters of a string
in thread Retain first 4 characters of a string

Sagar, the code works perfectly fine however is it possible to retain model numbers? e.g
Apple iPhone x41212 Black Cover
so even if its a mixture of a model number and letter the split would retain it as is? Thank you :)

Replies are listed 'Best First'.
Re^3: Retain first 4 characters of a string
by Anonymous Monk on Aug 24, 2012 at 12:32 UTC
    Yes , it is possible -- how would you do it? Please describe the steps in english
Re^3: Retain first 4 characters of a string
by uday_sagar (Scribe) on Aug 24, 2012 at 13:23 UTC

    I didnt get you exactly. Do you want to conserve (not trimming to 4 letters) the string "x41212"? (model number)

      Sagar, I have changed the requirement slightly, basically the colours below:
      baby blue baby pink black dark blue brown dark purple green orange hot pink light purple red white yellow
      If the space between them exists then how do i retain just the first letter of the words e.g hot pink would be hp and light purple would be lp? I can't figure out how to do it with the split and join if there is no space between two words then simply retain the colour i.e. red would be red Thank you :)
        $ perl -Mstrict -Mwarnings -E ' > open my $inFH, q{<}, \ <<EOF or die $!; > baby blue > baby pink > black > dark blue > brown > dark purple > green > orange > hot pink > light purple > red > white > yellow > light powder blue > EOF > > say for > map { > @$_ > 1 > ? join q{}, map { substr $_, 0, 1 } @$_ > : $_->[ 0 ] > } > map { [ split ] } > <$inFH>;' bb bp black db brown dp green orange hp lp red white yellow lpb

        Cheers,

        JohnGG

        Really? When you specify the number of times to match, enter 1