Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

array conversion to lower case

by Anonymous Monk
on Oct 10, 2012 at 05:38 UTC ( #998136=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

@array = ("ONE","TWO","THREE","ABC","DEF"); i want to convert all the elements in the @array in to lowercase my o/p should like as one two three abc def can any one help me about this

Comment on array conversion to lower case
Re: array conversion to lower case
by NetWallah (Abbot) on Oct 10, 2012 at 05:42 UTC
    Use a "for" loop, and the "print" and "lc" functions.

                 I hope life isn't a big joke, because I don't get it.

Re: array conversion to lower case
by Athanasius (Monsignor) on Oct 10, 2012 at 05:44 UTC

    Or use map:

    15:40 >perl -e "@array = ("ONE","TWO","THREE","ABC","DEF"); @array = m +ap { lc } @array; print join(', ', @array);" one, two, three, abc, def 15:40 >

    Hope that helps,

    Athanasius <°(((><contra mundum

Re: array conversion to lower case
by davido (Archbishop) on Oct 10, 2012 at 06:37 UTC

    What is the purpose? If it's for comparison, and there's a possibility of taking Unicode input, there may be a more robust answer. Consider instead using fc (fold case) if you are using Perl 5.16.x, or Unicode::CaseFold with earlier Perl versions to obtain a case-neutral basis for comparison.

    There are some good articles on the topic, but the following are probably among the best resources available:

    I'm probably over-thinking it though. It's possible you live in an ASCII-only world, and simply need lower case for some reason other than case-neutral comparisons, in which case, this post is irrelevant. ;)


Re: array conversion to lower case
by tobyink (Abbot) on Oct 10, 2012 at 09:36 UTC
    use Data::Dumper; @array = ("ONE","TWO","THREE","ABC","DEF"); $_=lc for @array; print Dumper \@array;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-09-20 09:09 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (157 votes), past polls