Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

perlnoobster,

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


Comment on Re: Retain first 4 characters of a string
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989472]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-07-22 21:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (129 votes), past polls