Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Retain first 4 characters of a string

by uday_sagar (Scribe)
on Aug 24, 2012 at 09:57 UTC ( #989472=note: print w/replies, xml ) Need Help??

in reply to Retain first 4 characters of a string


I just added flavors to your dish so that it is eatable and delicious!

$phn = "Apple iPhone 4 Black Cover"; print $trim = join '-', (map { substr($_, 0, 4) } split /\s+/, $phn);

Replies are listed 'Best First'.
Re^2: Retain first 4 characters of a string
by perlnoobster (Sexton) on Aug 24, 2012 at 10:08 UTC
    Perfect, it is indeed eatable and very delicous! Thank you to everyone that had a hand in helping me, i really appreciate it :)
Re^2: Retain first 4 characters of a string
by perlnoobster (Sexton) on Aug 24, 2012 at 12:25 UTC
    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 :)
      Yes , it is possible -- how would you do it? Please describe the steps in english

      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 :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989472]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2018-03-20 12:21 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (250 votes). Check out past polls.