Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

checking particular part of string

by sumandas (Initiate)
on Mar 07, 2014 at 12:01 UTC ( #1077367=perlquestion: print w/ replies, xml ) Need Help??
sumandas has asked for the wisdom of the Perl Monks concerning the following question:

I want to split the number part from string in windows strawbery perl

My code is

my @str=("MUXmh123","MUXho124"); foreach my $kplmr(@str) { my ($kplmrletter,$number) = split/\d/,$kplmr; my $strr=uc($kplmrletter)."-"; }

Comment on checking particular part of string
Download Code
Re: checking particular part of string
by hippo (Curate) on Mar 07, 2014 at 12:12 UTC

    To make it generic you would need a capture group. eg:

    echo sumanna | perl -pe 's/(na$)/uc($1)/e;'

    Is this what you were after?


    Update: for an example with more obvious genericism try:

    echo sumanna | perl -pe 's/(..$)/uc($1)/e;'
Re: checking particular part of string
by vinoth.ree (Parson) on Mar 07, 2014 at 13:35 UTC

    Hi sumandas,

    In your code you no need to use $str =~ m/na/ you can directly use the $str =~ s/na//gi; code, as you need to check the "na" string to be matched at the end of the srtring, perl has the special chater "$" used to match at the end of the sctring, so $str =~ s/na$//gi; this will match the "na" at the end of the string, now u want that "na" at the end of the string to be converted to UPPERCASE "na", so we need to get the same substring to replace, so we need to group it, as $str =~ s/(na)$/uc($1)//gi;, the grouped string will be available in $1 special variable, and use the uc() BIF to convert the matched substring to uppercase.


    All is well
Re: checking particular part of string
by kcott (Abbot) on Mar 08, 2014 at 00:05 UTC

    G'day sumandas,

    Please do not delete your post and just replace it with something different.

    Three monks have voluntarily given their time to try to help you. Because you've changed your post, their responses are meaningless and you have completely wasted their time!

    Please read "How do I change/delete my post?".

    Given your new question, this does what you ask:

    #!/usr/bin/env perl -l use strict; use warnings; my @strings = qw{MUXmh123 MUXho124 999999}; my @results = map { /(\D+)/ ? "\U$1-" : () } @strings; print for @results;

    Output:

    MUXMH- MUXHO-

    -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-12-18 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (61 votes), past polls