#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Unsure On Join And Map


    I'm not a perl programmer.

    I've got some code that comes from a perl file that I don't have. I was wondering if anyone could either explain what it does or provide an equivalent example in a different language (preferably in c or python).

    Code:
    sub ASCIItoUnicode{
        return join('', map {($_, "\0")} split(//, $_[0]));
    }
    ... This isn't homework in any way, shape or form.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    I would write that code as:

    Code:
    use Encode;
    
    sub ASCIItoUnicode {
        my $text = shift;
        return Encode::encode( 'UTF-16le', $text );
    }
    Which is easier to read, lets you use "perldoc Encode" to find out what it's doing, and won't choke if the input isn't actually ASCII.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    It is splitting the input (the first argument passed to the subroutine) into individual characters, adding a '0' value byte after each character and concatenates the result back into a string, the aim being to transform ASCII (or possibly UTF-8) input (one byte per character) into UTF-16 (two bytes per character).

    As said by OmegaZero, it is probably better to use existing Unicode modules such as Encode to do this.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    Read and analyze the statement from right to left. The result of each function is fed as input to the function on its left.

    More detailed explanation of what each of the functions do can be found here:
    perldoc -f split
    perldoc -f map
    perldoc -f join
    perldoc -f return

    Comments on this post

    • Laurent_R agrees
    Last edited by FishMonger; September 14th, 2013 at 08:28 AM.

IMN logo majestic logo threadwatch logo seochat tools logo