2 Replies - 6442 Views - Last Post: 14 September 2013 - 02:15 AM

#1 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Unsure on join and map

Posted 13 September 2013 - 01:19 PM

I'm not a perl programmer.

I've got some code that comes from a perl file. 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).

use strict;
use warnings;

sub ASCIItoUnicode{
    # I'm interested in this line
    return join('', map {($_, "\0")} split(//, $_[0]));
}

my $str = "the quick brown fox";
my $ret = ASCIItoUnicode($str);

print "it was: '", $str, "'\n";
print "it  is: '", $ret, "'\n";

Output:
[email protected]:~>perl test.pl
it was: 'the quick brown fox'
it  is: 'the quick brown fox'



... This isn't homework in any way, shape or form.

This post has been edited by gl3thr0: 13 September 2013 - 02:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Unsure on join and map

#2 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Unsure on join and map

Posted 13 September 2013 - 04:43 PM

some reading later. I believe it pads the ascii characters with null bytes to create 2 byte characters.
Was This Post Helpful? 0
  • +
  • -

#3 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Unsure on join and map

Posted 14 September 2013 - 02:15 AM

Well... it attempts to do what you deduced after your extra reading. But it fails.

First off, here's what it does and how it does it:

View Postgl3thr0, on 13 September 2013 - 09:19 PM, said:

    return join('', map {($_, "\0")} split(//, $_[0]));


This line does three things, operating from right to left.
  • Take the first parameter passed to the function ($_[0]) and split it into a list of individual characters. "The" becomes (T, h, e).
  • map transforms the list, replacing each list element with ($_, "\0"), which means "the original list element followed by a new element containing a null byte". (T, h, e) becomes (T, \0, h, \0, e, \0).
  • join combines all the elements of the list into a single string, inserting an empty string between each pair of elements. (T, \0, h, \0, e, \0) becomes "T\0h\0e\0".


As you might have noticed, the end result is not a string of three 16-bit characters. It's a string of six 8-bit characters. This may or may not be close enough for your purposes.

perl -MData::Printer -E '$s = "The"; p $s; $s2 = join("", map {($_, "\0")} split(//, $s)); p $s2;'
"The"
"T\0h\0e\0"


(Using the p function from Data::Printer rather than builtin print because it shows both printable and unprintable characters. Unfortunately, that means you won't be able to run that command yourself unless you have Data::Printer installed.)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1