Tag: array’

Javascript สั้นๆ ตอน Array ปลอมๆ [แล้วจะรู้ได้ยังไงว่าเป็น Array หรือ Object]

 - by NAzT

ใน ไม่มีข้อมูลที่เป็น จริงๆ

Array ที่มีอยู่มัน array แบบหลอกๆ จริงแล้วมันเป็น ที่มี key เป็นตัวเลข, มี method เกี่ยวกับ array, และมี property length

ถ้าเราพึ่งคำสั่ง typeof อย่างเดียวเราก็จะเจ็บอีกตามเคย

javascript-phony-array

กล่าวคือถ้าเราสั่ง typeof [] และ typeof {} มันจะบอกว่า เป็น object ทั้งคู่! แบบนี้
NAzT_s Blog Read this article »

Unit Vectors – Normalizing ใช้ python และ numpy

 - by Nat

 

ก่อนที่เราจะ normalize vector ได้ เราต้องหา norm ( magnitude หรือ ขนาด) ของมันก่อน

ซึ่งมันหาได้โดยการ ถอด Square root ของ ผลบวกกำลังสองครับ (sumsquare)

    length = sqrt((ax * ax) + (ay * ay) + (az * az))
    length = sqrt(9 + 1 + 4) = 3.742

ซึ่ง 3.742 จะเป็น ขนาดของ Vector [1,2,3] ครับ

ถ้าจะ normalize  vector  นี้ก็เอา magnitude ไปหารทุกตัวครับ

    x = 3.0 / 3.742 = 0.802
    y = 1.0 / 3.742 = 0.267
    z = 2.0 / 3.742 = 0.534

 

ซึ่งใน python เขียนแค่

ก็จะได้คำตอบ

([ 0.26726124,  0.53452248,  0.80178373])

Read this article »

[Google Maps Geocoding] ลอง Parse Json ด้วย Groovy + Java (Netbeans)

 - by Nat

โปรเจ็คตลกๆมาอีกแล้ว  เพราะโปรเจ็คนี้ผมใช้  Json Lib + + Java

แต่ผมใช้ Groovy ดึง json (URL.text) แล้วใช้ Java เป็นตัวแกะ jsonObject เป็น jsonArray

หลังจากนั้นเอาข้อมูลโยนกลับไปให้ Groovy ถอดเอาเฉพาะส่วนที่ต้องการออกมา (ในที่นี้คือ coordinates — latitude ,longitude นั่นแหละ)

ดูวุ่นวายมาก ทั้งๆที่ เขียน Groovy อย่างเดียวก็ได้ เพราะ Json Lib ก็ลองรับ groovy  หรือจะเขียน Java เพียวๆ เลยก็ได้ 

 

เรื่องของเรื่องคืออยากเขียน Java ร่วมกับ Groovy อีกครั้ง เพราะไม่ค่อยถูกกับ Java จริงๆ

มันดูเวื่นเว้อ  มันเขียนเวิ่นเว้อด้วย ยาวๆ งงๆ (อาจเป็นเพราะไม่ได้ศึกษาอย่างลึกซึ้ง)

groovy มันสั้น เข้าใจง่าย ไม่เวิ้นเว้อ (อาจจะใช้ได้ไม่เต็มประสิทธิภาพ แต่ผมชอบ พวก Closures )

 

เหตุผลที่ไม่เขียน Groovy เพียวๆไปเลยก็เพราะว่า 

code ตัวอย่างที่เค้ามีให้ดู มันพิมพ์ผิด 

def fromList = [1,true,'json'as JSONarray  

มันควรจะต้องเป็น JSONArray  เมื่อแก้และเอาอันอื่นมาลองดู มัน cast เป็น Type พวก net.sf.json.* ไม่ได้ อาการประมาณนี้ 

ก็เลยลองซัด Java + Groovy ไปเลย

มันก็เจอปัญหานิดหน่อยเพราะ ไม่ได้อ่าน Dependency List เลยค่อยๆตามหา พอได้ครบแล้วเพิ่งจะเห็นว่ามันมีบอกไว้ว่า 

Json-lib requires (at least) the following dependencies in your classpath:

  • jakarta commons-lang 2.4
  • jakarta commons-beanutils 1.7.0
  • jakarta commons-collections 3.2
  • jakarta commons-logging 1.1.1
  • ezmorph 1.0.6

น่าเขกกะโหลกจริงๆ Read this article »

[code] อัพโหลดภาพเข้า twitpic โดยใช้ api

 - by NAzT

[code] อัพโหลดภาพเข้า twitpic โดยใช้ api จะใช้

ซึ่ง จาก function จะ input username ,password , filepath

function postTwitpic($username='',$password='',$filepath='')
{
    $postfields = ();
    $postfields['username'] = $username;
    $postfields['password'] =  $password;
    $postfields['media'] = "@$filepath";
    $twitter_url = 'http://twitpic.com/api/upload';
    $curl = curl_init();
    curl_setopt($curl, _CONNECTTIMEOUT, 2);
    curl_setopt($curl, _HEADER, false);
    curl_setopt($curl, _RETURNTRANSFER, 1);
    curl_setopt($curl, _BINARYTRANSFER, 1);
    curl_setopt($curl, _URL, $twitter_url);
    curl_setopt($curl, _POST, 3);
    curl_setopt($curl, _POSTFIELDS, $postfields);
    $result = curl_exec($curl);
    curl_close($curl);
    $login_ = new SimpleXMLElement($result);
    if (isset($login_->error)) {
        print_r($login_);
        return false;
    } else
    {
        return $login_;
    }
}
function return message ที่โพส ยังไง print_r ดูได้ครับ

[Draft] : NPDigger Version Semi Auto

 - by NAzT

กำลังทำ npdigger version ใหม่แบบ Semi Automatic เปิดโปรแกรมทิ้งไว้ แล้วมันจะโพสเองเรื่อยๆ

จด code  ไว้หน่อยละกัน

function getCode($userkey)
{ if(isset($userkey)) { // $this->db->select_min(‘added_time’); // $this->db->where(‘status’, ); // $query= $this->db->get(‘captcha’); /// $this->db->select(‘UNIX_TIMESTAMP(MIN(added_time)) AS MIN_STAMP’); // $query= $this->db->get_where(‘captcha’, (‘userkey’ => $userkey) ); $this->db->select(‘captcha, decaptcha’); // $this->db->select_min(‘addfed_time’); // $array = array(‘UNIX_TIMESTAMP(added_timed)’ => ‘UNIX_TIMESTAMP(MIN(added_time))’); $where = “UNIX_TIMESTAMP(added_time) =  (select UNIX_TIMESTAMP(MIN(added_time))  FROM captcha WHERE userkey=’$userkey’) “; $this->db->where($where); $data_array['query']=$this->db->get_where(‘captcha’, array(‘userkey’ => $userkey)); $this->load->view(‘digger_getCode’, $data_array); echo “last”; // print_r($data_array['query']); }

Related Blogs

    getPage และ postPage ด้วย php+cURL

     - by NAzT

    มีฟังก์ชั่น 2 อัน ชื่อตรงตัวเลยครับว่าให้ทำอะไร วิธีใช้ก็อ่านเอาที่ Prototype ของ function ได้เลยครับ

    //-----------------------------------------------------------------------------------
    function ($url, $referer, $timeout, $header){
    if(!isset($timeout))
    $timeout=30;
    $ = _init();
    if(strstr($referer,"://")){
    _setopt ($, _REFERER, $referer);
    }
    curl_setopt ($curl, _URL, $url);
    curl_setopt ($curl, _TIMEOUT, $timeout);
    curl_setopt ($curl, _USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
    curl_setopt ($curl, _HEADER, (int)$header);
    curl_setopt ($curl, _RETURNTRANSFER, 1);
    curl_setopt ($curl, _SSL_VERIFYPEER, 0);
    $html = curl_exec ($curl);
    curl_close ($curl);
    return $html;
    }
    //-----------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------
    function postPage($url,$pvars,$referer,$timeout){
    if(!isset($timeout))
    $timeout=30;
    $curl = curl_init();
    $post = http_build_query($pvars);
    if(isset($referer)){
    curl_setopt ($curl, _REFERER, $referer);
    }
    curl_setopt ($curl, _URL, $url);
    curl_setopt ($curl, _TIMEOUT, $timeout);
    curl_setopt ($curl, _USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
    curl_setopt ($curl, _HEADER, 0);
    curl_setopt ($curl, _RETURNTRANSFER, 1);
    curl_setopt ($curl, _SSL_VERIFYPEER, 0);
    curl_setopt ($curl, _POST, 1);
    curl_setopt ($curl, _POSTFIELDS, $post);
    curl_setopt ($curl, _HTTPHEADER,
    ("Content-type: application/x-www-form-urlencoded"));
    $html = curl_exec ($curl);
    curl_close ($curl);
    return $html;
    }
    //-----------------------------------------------------------------------------------
    จาก  http://www.tellinya.com  คร้าบ

    Related Blogs

    MPI Programming : ตอนที่ 2

     - by NAzT

    Blocking Communication

    อันนี้เป็น Prototype ของ MPI_Send และ MPI_Recv ซึ่งเป็นการสื่อสารแบบ Blocking ครับ (มันมี Non-blocking ด้วยไว้ค่อยมาคุยกันอีกทีว่ามันต่างกันยังไงในวันหลังนะครับ)

    ที่ยกเรื่องนี้มาให้ดูก่อน เพราะตอนหน้าเราจะส่งข้อมูลข้าม Process โดยใช้ MPI (Send & Recv) กันแล้วคร้าบผม

    int MPI_Send( void *buf,
                  int count,
                  MPI_Datatype datatype,
                  int dest,
                  int tag,
                  MPI_Comm comm
    );

    Read this article »

    Ruby Array with Range : Note

     - by NAzT

    กำลังสงสัยว่า

     ใน ruby a[0..n] ทำไมมันถึงไม่เหมือนกับ a[0,n] ครับ มันควรจะเหมือนกันไม่ใช่หรอครับ   เพราะไปเขียน Merge Sort แล้ว มันให้ผลไม่เหมือนกันเกิด Infinite Loop เลยทีเดียว พอเปิดหนังสือดู อ้าว… เฮ้ย เจ้าตัว operator [ ] ของ มันก็ให้ผลเหมือนกันนี่หว่า return เหมือนกันเลย หลังจากนั้นก็ลองไปเทสแยกดู ตอนแรก a[0,3] กับ  a[0..3] มันเท่ากัน ก็เลย งงว่า มันก็เท่ากันนี่หว่า แต่ลองเช็คอีกทีว่าถ้าเป็น 0,0 มันจะ return อะไร แล้วก็โป๊ะเชะ 

     

    ที่มันไม่เท่าเพราะ  a=["a","b","c","d"] แล้ว   >> a[0..0] => ["a"] ส่วน    >> a[0,0]  => []

     

     

    เฮ่อแบบนี้ก็ต้องใช้ a[x,y]  เพราะมันจะ return [] มาได้ทำให้หลุดจาก recursion 

     

    Related Blogs