gurpreetsingh13 has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
Was just trying some statements and wondered why a similar one doesn't work.I might be wrong on some basics. But why the 2nd statement fails. I mean if such a thing works, then we can directly use it to print a single element returned from a function or method(like in Java).e.g.print ("adf","ff","f"); # Gladly prints the list print ("adf","ff","f")[1]; #Gives a syntax error $val=("adf","ff","f")[1];print $val; #Works easily print join "",("adf","ff","f"); #Works print join "",("adf","ff","f")[1]; #Works since treated in list contex +t
Similarly in Java we can do:(sort {$a<=>} @lst)[0] # an alternative way to get min value from some + numeric list.
obj.method()[2] #when we know that method returns an array
Tried to search a little about this, but could not find a reasonable thread for the same. Sorry again if this is some conceptual weakness of mine.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Getting single element from an array
by choroba (Cardinal) on Nov 01, 2012 at 09:20 UTC | |
Re: Getting single element from an array
by Anonymous Monk on Nov 01, 2012 at 09:16 UTC | |
Re: Getting single element from an array
by Anonymous Monk on Nov 01, 2012 at 09:19 UTC | |
Re: Getting single element from an array
by gurpreetsingh13 (Scribe) on Nov 01, 2012 at 09:36 UTC | |
Re: Getting single element from an array
by itnomad (Scribe) on Nov 04, 2012 at 02:27 UTC | |
Re: Getting single element from an array
by brx (Pilgrim) on Nov 05, 2012 at 17:31 UTC |
Back to
Seekers of Perl Wisdom