Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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)."-"; }

Replies are listed 'Best First'.
Re: checking particular part of string
by hippo (Canon) 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 kcott (Chancellor) 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;



    -- Ken

Re: checking particular part of string
by vinoth.ree (Monsignor) 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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1077367]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-06-22 21:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.