On Wed, Feb 16, 2011 at 12:21 AM, botp <[email protected]> wrote:

>
> another example, if i may:
>
> let's say i have an array a
>
>  a=["botp", "x", "1000", "1000", "botp,,,", "/disk2_2/home/botp",
> "/bin/bash"]
>
> and i want to assign the 1st element to name, 3rd elem to userid,
> 2nd-to-last-element to home, and last element to shell
>
> then using parallel assignment, just do
>
>  name, _, userid, *_ , home, shell = a
>
> instead of the usual
>
>  name = a[0]
>  userid = a[2]
>  home = a[-2]
>  shell = a[-1]
>
> note here i'm already using the negative indexes feature to access
> elements from behind
>
>
> > On a similar note, I do lurv using the ternary operator in PHP [1] -
> > but that's because I'm too lazy to write a whole if-else block when I
> > can do it with one line. It also makes me look cool sa mga newbies.
>
> i think ternary op was popularized by C.  i use them, but i don't nest
> them, otherwise i'd use the block mode (yes, paranoid and not cool of
> me :-)
>
> best regards -botp
>
>
>From the example :) I can't do the same style, and gives out an error. So I
had to do multiple assignment directly and changed the fifth variable's name
since * is reserved.

Since there's only one variable being passed multiple values, the rest were
discarded, and when print was invoked to output the values of the other
variables, they were given nil values. :)

> a="botp", "x", "1000", "1000", "botp,,,", "/disk2_2/home/botp",
"/bin/bash"
> name, _, userid, gid, *_ , home, shell = a
stdin:1: unexpected symbol near '*'
> name, _, userid, gid, \*_ , home, shell = a
stdin:1: unexpected symbol near '\'
> name, _, userid, gid, __ , home, shell = a
> print(name, _, userid, gid, __ , home, shell)
botp nil nil nil nil nil nil
>
>
> name, _, userid, gid, __ , home, shell = "botp", "x", "1000", "1000",
"botp,,,", "/disk2_2/home/botp", "/bin/bash"
> print(name, _, userid, gid, __ , home, shell)
botp x 1000 1000 botp,,, /disk2_2/home/botp /bin/bash
>

Weird but lingaw :)



-- 
-------------------------------------------------------------
"Penguin, penguin, and more penguin !"

http://www.webhostadmins.com
http://www.madforubuntu.com
http://baudizm.blogsome.com
_________________________________________________
Kagay-Anon Linux Users' Group (KLUG) Mailing List
[email protected] (http://lists.linux.org.ph/mailman/listinfo/klug)
Searchable Archives: http://archives.free.net.ph

Reply via email to