Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

And here it is, a bunch

#!/usr/bin/perl -- use strict; use warnings; use 5.010; use Data::Dumper::Names; my $orig = 'Apple iPhone 4 Black Cover'; my $want = 'Appl-iPho-4-Blac-Cove'; say Dumper $orig, $want; my $trim = join ' ', (split /\s+/, $orig)[0..1]; say Dumper $trim; say "THE PROBLEM WITH YOUR ATTEMPT IS RETURN VALUE OF SPLIT ", Dumper( +[ split /\s+/, $orig ]); say "OF WHICH YOU ONLY TAKE FIRST 2 ", Dumper([ (split /\s+/, $orig)[0 +..1] ]); say "YOU NEED MATCH OPERATOR AND MAP AS WELL"; $trim = join '-', map { /^(\w{1,4})/ } split /\s+/, $orig; say Dumper $want, $trim ; say "SAME THING USING SUBSTITUTION OPERATOR"; $trim = $orig; $trim =~ s{ (?: (\w{1,4}) # capture first 1-to-4 word chars in $1 \w* # match and ignore any remaining words ) | # or (\s+) # any save any-amount-of-whitespace into $2 }{ defined $2 # if defined $2 ? '-' # then return '-' : $1; # else return $1 }gex; say Dumper $want, $trim ; $trim = $orig; say "AND MAYBE FASTER, first transliterate spaces into -"; $trim =~ tr/\t\r\n /-/s; say Dumper $trim; say "then shorten words to 4 chars using perl 5.10 feature \\K"; $trim =~ s{\w{4}\K\w*}{}g; # ignore first 4 word chars, delete others say Dumper $want, $trim ; say "USING MATCH OPERATOR ONLY "; $trim = join '-', $orig =~ m{(\w{4})\w*\b|(\w{1,3})}g; say Dumper $want, $trim ; say "THOSE CAPTURE GROUPS ARE TRICKY "; $trim = join '-', grep defined, $orig =~ m{(\w{4})\w*\b|(\w{1,3})}g; say Dumper $want, $trim ; __END__ $ perl trim.pl $orig = 'Apple iPhone 4 Black Cover'; $want = 'Appl-iPho-4-Blac-Cove'; $trim = 'Apple iPhone'; THE PROBLEM WITH YOUR ATTEMPT IS RETURN VALUE OF SPLIT $VAR1 = [ 'Apple', 'iPhone', '4', 'Black', 'Cover' ]; OF WHICH YOU ONLY TAKE FIRST 2 $VAR1 = [ 'Apple', 'iPhone' ]; YOU NEED MATCH OPERATOR AND MAP AS WELL $want = 'Appl-iPho-4-Blac-Cove'; $trim = 'Appl-iPho-4-Blac-Cove'; SAME THING USING SUBSTITUTION OPERATOR $want = 'Appl-iPho-4-Blac-Cove'; $trim = 'Appl-iPho-4-Blac-Cove'; AND MAYBE FASTER, first transliterate spaces into - $trim = 'Apple-iPhone-4-Black-Cover'; then shorten words to 4 chars using perl 5.10 feature \K $want = 'Appl-iPho-4-Blac-Cove'; $trim = 'Appl-iPho-4-Blac-Cove'; USING MATCH OPERATOR ONLY Use of uninitialized value $orig in join or string at trim.pl line 42. Use of uninitialized value $orig in join or string at trim.pl line 42. Use of uninitialized value $orig in join or string at trim.pl line 42. Use of uninitialized value $orig in join or string at trim.pl line 42. Use of uninitialized value $orig in join or string at trim.pl line 42. $want = 'Appl-iPho-4-Blac-Cove'; $trim = 'Appl--iPho---4-Blac--Cove-'; THOSE CAPTURE GROUPS ARE TRICKY $want = 'Appl-iPho-4-Blac-Cove'; $trim = 'Appl-iPho-4-Blac-Cove';

In reply to Re^3: Retain first 4 characters of a string by Anonymous Monk
in thread Retain first 4 characters of a string by perlnoobster

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2015-07-04 09:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (59 votes), past polls