This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Scripting 101

Chat log Class 3

hemanth hm's picture
Sat, 2010-09-25 19:46

hemanth>    Hello hackers :)
< SourRust>    hey
hemanth>    Hows it going?
< jmuniz>    Yo! Hello everybody
hemanth>    anyone trying limpid btw :) ?
< adam_collado>    hey everybody
hemanth>    hey adam_collado
hemanth>    before i start, i need to clarify few details
hemanth>    4 commits with 120 additions and 0 deletions -> datruth29
hemanth>    4 commits with 94 additions and 0 deletions  -> sourrust
hemanth>    3 coomits  with 14 additons and 0 deletions -> josemuniznyc
hemanth>    were the pull requests i received
hemanth>    congrats on that, but i just want you all to go through http://github.com/hemanth/p2pu/issues#issue/2
hemanth>    as the pull request is from your master to the p2pu master
< jmuniz>    Got it
hemanth>    there will be conflicts if the filenames are same
< jmuniz>    right
< adam_collado>    ohhhh, that makes sense
hemanth>    two options, pushing new branch, or naming files diff; the second on seems more apt for us, as sharing would be easier
hemanth>    and less of switch
< SourRust>    yea I changed the file names
hemanth>    so do take your time to fix them
hemanth>    as of now datruth29's code is in, has he would also do this small exercise for now and then onwards all will be fine
  hemanth has changed the topic to: Variables continued
< adam_collado>    okay
hemanth>    So, starting with todays class...
adam_collado>   i should clarify hemanth, that i'm datruth29
< adam_collado>    i should probably just change my name going forward
hemanth>    adam_collado, :) nice i will remember
hemanth>    last class we had stooped with python variables
hemanth>    i would suggest you to see http://github.com/hemanth/p2pu/raw/master/variables/python/variables.py
hemanth>    that is a neat and simple code from adam_collado as you can see there he has tried all different data types
< jmuniz>    sweet!
hemanth>    but i would strongly suggest to use GNU GPLv3 for our own benefit
hemanth>    uokesita was to first one to start of with H.W
hemanth>    Osledy Bazo is not here in the IRC i blv...
  hemanth has changed the topic to: RUBY VARIABLES
hemanth>    In ruby we have global, local, instance and constant variables
hemanth>    $,[a-z] or _,@,[A-Z] respectively
hemanth>    we also have @@ class variables
hemanth>    let see few examples
hemanth>    to make ourselves clear
hemanth>    so all of you open your terminal and those who are on limpid its already opened for you by default ;)
hemanth>    type in irb
hemanth>    tell me once ready
< SourRust>    ready
< jmuniz>    rdy
hemanth>    SourRust, Jmuniz, you on limpid :) ?
< jmuniz>    nope. just a basic term
hemanth>    ok :)
hemanth>    others you ready?
< SourRust>    nah i have terminal open already
hemanth>    i would appreciate if you could give it a try later on
< jmuniz>    will do
< SourRust>    k
hemanth>    hope others are also ready by now
hemanth>    irb(main):010:0> $name="p2pu"
hemanth>    irb(main):010:0> $num=420
hemanth>    irb(main):010:0> $unvi=['p','2','p','u']
hemanth>    irb(main):010:0>  puts $name
hemanth>    irb(main):010:0> print $name
hemanth>    let the same with others to notice, how puts and print works
hemanth>     /let/try ^
< jmuniz>    what does the nil at the end mean?
hemanth>    that's irb indicating end
< jmuniz>    ah! cool
hemanth>    irb(main):010:0> print $unvi[0]
hemanth>    irb(main):010:0> print $unvi[-1]
hemanth>    irb(main):010:0> print $unvi[0-2]
hemanth>    noticed the magic :) ?
< SSDevMonkey>    Sorry for being late, modem decided it didn't want to cooperate this morning.
< jmuniz>    -1 is going backwards?
hemanth>    jmuniz, correct
hemanth>    CSSDevMonkey, no issues
< jmuniz>    cool
hemanth>    to access we need to use #{$var}
hemanth>    as its irb $var will print the data
hemanth>    lets make it more clear
hemanth>    irb(main):027:0> trace_var :$x, proc{puts "$x is now #{$x}"}
hemanth>    irb(main):028:0> $x=5
hemanth>    what do u notice ?
hemanth>    ...
< adam_collado>    when you place in a new value for x
< adam_collado>    it displays the message you put in the proc
< jmuniz>    when I change it to 10 it changes the msg
< jmuniz>    oops, too late
hemanth>    yup
hemanth>    that just to demonstrate #{$x}
hemanth>    that is #{$varname}
hemanth>    must be use to fetch the value of the var
hemanth>    that class @@ and the instant variables will be covered when we take yup Object Orientation class
hemanth>    cos as of now, i want to keep it simple and plain to normal low level abstractions
hemanth>    irb(main):030:0> self="my_self"
hemanth>    try that
< SourRust>    error
hemanth>    irb(main):030:0> nil="none"
hemanth>    irb(main):030:0> true="false"
hemanth>    irb(main):030:0> false="true"
hemanth>    noticed them?
< jmuniz>    yep
hemanth>    so one can't assign values to these, because
hemanth>    self => Execution context of the current method.
hemanth>    nil => The sole-instance of the NilClass class. Expresses nothing.
hemanth>    true => The sole-instance of the TrueClass class. Expresses true.
hemanth>    false => The sole-instance of the FalseClass class. Expresses false.
hemanth>    Now. do worry much about words like instance, class, methods all this will be covered as we move on, to Object Orientation
hemanth>    as of now, just bare this in mind and learn to define and declare simple variables
hemanth>    irb(main):032:0> foo = 44; puts foo; defined?(foo)
hemanth>     defined?  is an operator which checks whether an identifier is defined. It returns a description of the identifier if it is defined, or nil otherwise.
< jmuniz>    local-variable
hemanth>    jmuniz, yup, its a local-variable
hemanth>    So, if everything is fine with thus so far, we can make a move to perl variables!
hemanth>    Any queries?
< adam_collado>    what was this limipid that you were talking about eariler?
hemanth>    rise your hand o/ if any, then type, so i don assume that there are none and move on
< jmuniz>    One... sometimes we say $name and sometimes we say foo
< jmuniz>    o/
< jmuniz>    which is for what?
hemanth>    adam_collado, you didn't get the broadcast mail from the group?
< adam_collado>    hm, i'll check my mail now
hemanth>    jmuniz, $ indicates global variable, that is can be accessed anywhere in the program
< jmuniz>    I didn't get an email about limpid
< jmuniz>    got it
< adam_collado>    got it
hemanth>    jmuniz, you got the mail, or clarification to the question ;) ?
< adam_collado>    that's awesome
hemanth>    adam_collado, thanks, that's just beta, will improve upon the issues you guys rise :)
< jmuniz>    haha... no email, but I understood the answer
hemanth>    nice
manth more on limpid http://www.h3manth.com/content/limpid-simple-app-p2pu
< jmuniz>    sweet
manth thanks hobs for taking part in the experiment and sharing space on his server to stream media
hemanth>    Saying it again, "So, if everything is fine with thus so far, we can make a move to perl variables!"
< adam_collado>    sure
< vibha6bhat>    I am fine with it :)
hemanth>    kool
< jmuniz>    rdy
  hemanth has changed the topic to: PERL VARIABLES
hemanth>    Come out of irb, Ctlr^D and type in 'psh' and hit enter
hemanth>    Perl has two types of variables, scalar and vector
hemanth>    scalar variables take only one value
hemanth>    one value at a time like, num=4
hemanth>    vector takes multiples values, that is simply an array or hash
hemanth>    lets see examples
hemanth>    psh% $number = 4
hemanth>    psh% print $number
hemanth>    you must see something like 4psh%
hemanth>    but i want something like
hemanth>    4
hemanth>    psh%
hemanth>    how do i do it ;) ?
hemanth>    very simple for you guys, as i could notice the brightness in the first class itself
< SSDevMonkey>    i'm using my Mac right now, and i typed in 'psh' and hit henter, and it says  -bash: psh: command not fond
hemanth>    CSSDevMonkey, at the end of last class, it was request to install psh
hemanth>    CSSDevMonkey, try sudo brew install psh
hemanth>    CSSDevMonkey, if in darwin, try %  cd /opt/local/bin/portslocation/dports/psh
hemanth>    %  sudo port install psh  
< vibha6bhat>    print "$number\n"
hemanth>    vibha6bhat, correct, its just a new line character
< vibha6bhat>    yeah
hemanth>    psh% $myname = "hemanth";
hemanth>    psh% @array = ("value00","value01","value02");
hemanth>    psh% %hash = ("Quarter", 25, "Dime", 10, "Nickle", 5);
hemanth>    trying them?
< SourRust>    yup
< vibha6bhat>    yup, how do we print value of hash?
< jmuniz>    the hash is like a python dict?
< adam_collado>    i was just about to ask that
< vibha6bhat>    I did %hash["Quarter"] and got an error
hemanth>    vibha6bhat, print %hash
hemanth>    jmuniz, yes, you are right
< vibha6bhat>    yeah did that , but that gives whole hash
< vibha6bhat>    if i want value of one specific key, then..?
< vibha6bhat>    value of one key *
hemanth>    making it generic $hash{key}
< adam_collado>    so when i try to print out the hash entries and make a new line => print "%hash\n"
< adam_collado>    it just prints out %hash
< adam_collado>    where as for the array and string, it prints out the values and makes a new line
< jmuniz>    print hash%{key}?
hemanth>    print $hash{key}
< jmuniz>    oops
< jmuniz>    haha
hemanth>    adam_collado, lets see another example for your case
< adam_collado>    so when i entered print "@array\n"
< adam_collado>    i get
< adam_collado>    value00 value 01 value02
< adam_collado>    same for $myname and $number
hemanth>    @array will just give you @array[0]
hemanth>     @array and  @array[0] are same
hemanth>    print "@array\n" you are getting value00 value01 value02 is it?
< adam_collado>    yep
< adam_collado>    it's not giving me the first entry, it's giving me all entries
< vibha6bhat>    it should give all enteries right
hemanth>    right
hemanth>    % and @ missed questions, made some mess go on with my head
hemanth>    making it all clear again
hemanth>    @100 = (1 .. 100);
hemanth>    print @100[0]
hemanth>    print @100
hemanth>    print @100[-1]
hemanth>    print "Size: ",scalar @100,"\n";
hemanth>    @array will return a list of all the elements contained in it.
< adam_collado>    whoa, you can declare numbers as varibles in perl??
hemanth>    :O)
hemanth>    yup
hemanth>    print "Max Index: ", $#array,"\n";
hemanth>    we can use push() to add an element to the end of an array.
hemanth>    like
hemanth>    @coins = ("Quarter","Dime","Nickel");
hemanth>    push(@coins, "Penny");
hemanth>    print @coins
hemanth>    pop() => removes the last element of an array.
hemanth>    pop(@coins);
hemanth>    shift() => removes the first element of an array.
hemanth>    unshift() => adds an element to the beginning of an array.
hemanth>    fine?
< SourRust>    all is good
< jmuniz>    got it
< adam_collado>    ye
< adam_collado>    yep
hemanth>    %ages = ('hemanth' => 7,'adam' => 11,'vibha6bhat' => 4, 'SourRust' => 12);
manth no issues with the ages i hope ;)
manth making hash more lucid
< adam_collado>    lol
hemanth>    print "Hemanth is $ages{hemanth} years old\n";
hemanth>    print "Ages of everyone ",join(', ',values %ages),"\n";
hemanth>    finding it meaningful?
hemanth>    hashes are nothing but key,value pairs
< adam_collado>    of course, just a little disorienting. then again, i've feel that way every time i look at perl code....
hemanth>    join(', ',values %ages) => will attach comma ',' to each of the values of %ages (hash)
hemanth>    adam_collado, :)
hemanth>    any queries ?
< SourRust>    nope
hemanth>    o/ and type the queries if any :)
hemanth>    So, this ends a very brief intro of variables on BASH,Python,Ruby and Perl
< vibha6bhat>    o/
hemanth>    yes please
< vibha6bhat>    wats the diff b/w $ and % usage?
< vibha6bhat>    $ => global
< vibha6bhat>    anything specific with %?
hemanth>    you have confused $ => global of ruby and % hash of perl ?
hemanth>    vibha6bhat, or are you referring to something else
< vibha6bhat>    yes
< vibha6bhat>    $ is global only in ruby huh?
hemanth>    $ages{hemanth} is how we access the value
hemanth>    in perl  $ages{hemanth} is how we access the value
hemanth>    in ruby $varname indicates global and #{$varname} is how we access the values
< jmuniz>    ok. got it
hemanth>    guys need a suggestion from you all, if its fine for you to study all of them together or you want one at a time, for all vote 0 and one at a time vote 1
< jmuniz>    0 (makes the topics easier)
< vibha6bhat>    0
< SourRust>    0
< SSDevMonkey>    0
hemanth>    adam_collado, ?
< adam_collado>    0
< adam_collado>    sorry, was afk
hemanth>    wow nice :)
hemanth>    hobs, ?
hemanth>    So as a majority, comparison is good :)
hemanth>    So, if you are still muddled with variables, don worry, we will be using them always, so it will slowly stick to our heads :)
< jmuniz>    I do have to run now, but I'll make sure to check out the logs. See you guys tomorrow.
hemanth>    So, lets put an end for this class now and tomo we will be starting with conditional execution
< jmuniz>    sweet!
hemanth>    If you have queries, on other issues, please do ask
hemanth>    please do check out limpid, post issues on github
hemanth>    do you home works ;)
hemanth>     /you/your
< SourRust>    ok cya
< vibha6bhat>    ok :) thanks :) bye :)
< adam_collado>    later guys!
hemanth>    nice, cya all tomo
hemanth>    Logs will be up in 5-10mins
< vibha6bhat>    ok
< vibha6bhat>    :)