Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

checking particular part of string

by sumandas (Initiate)
on Mar 07, 2014 at 12:01 UTC ( [id://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 (Bishop) 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 (Archbishop) 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

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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-18 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found