Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6092475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:26:48+00:00 2026-05-23T12:26:48+00:00

I am trying to translate a python script to perl but currently stuck at

  • 0

I am trying to translate a python script to perl but currently stuck at couple of code snipets.

For instance how to write following python code in perl? (without using any perl array related packages)

# packet here is a binary string
data = array.array( 'h', ( ord( packet[i] ) for i in range( start, end ) ) )
FILE.write(data);

My (failed) attempt:

my @data;
foreach $i ( $start .. $end ) {
   $data[$i] = ord( substr( $packet, $i, 1) );  # Assuming that perl's ord = python's ord
}

print FILE @data;

@ysth, the actual input is a large binary file and in excitment of posting my first question here I forgot to generate a sample input and ouput, sorry about that. For completeness find below the sample input and output.

input: [‘a’, ‘b’, ‘c’, ‘d’] #think this as packet contents, start=0, end=4

ouput in python:

> cat py.out | xxd
0000000: 6100 6200 6300 6400                      a.b.c.d.

output in perl:

> cat pl.out | xxd
0000000: 3937 3938 3939 3130 30                   979899100

@Pedro Silva, As I understand in first line of your suggestion you just replace for loop with map functionality, the effect is same.
The unpack ‘W*’ returns error as W is not a valid type (perl 5.8.8).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:26:49+00:00Added an answer on May 23, 2026 at 12:26 pm

    edit

    You need to clarify what you’re saying. On my python (2.7.2), this is wrong:

    FILE.write(data)
    

    because data needs to be a string. I think what you mean to say instead is:

    data.tofile(FILE)
    

    Anyway, is this what you want?

    print pack("s*", unpack("C*", "abcd"))
    # piped through xxd => 0000000: 6100 6200 6300 6400     a.b.c.d.
    

    Have a look through perldoc -f pack for the correct template string to use. Since you don’t have access to W (unsigned wchar), i’m using C (unsigned char). s is a signed short (corresponding to python’s h.)

    /edit

    There are no typed arrays in Perl. ord returns the numeric value of the first character of its argument.

    Assuming this is what you want, just map the list resulting from splitting $packet on each character to @data, transforming each element with ord (the argument $_ is implicitly given.)

    my @data = map { ord } split //, $packet; # or
    my @data = unpack 'W*', $packet;          
    

    Optionally, using a range different from the actual lower and upper bounds on $packet:

    (split //, $packet)[$start .. $end];    # or
     split //, substr $packet, $start, $end # ysth's version
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to translate a Perl script to PHP and I'm having trouble with
Sometimes I still get stuck trying to translate procedural code into functional code. Is
I'm trying to translate code from this thread in python: import sys from PyQt4.QtCore
I'm trying to translate some python code to haskell. However I reached a point
I have some Python code I'm trying to translate to Obj-C/Cocoa. It requires a
I have an app I'm trying to translate which uses tkinter and python, but
I'm trying to translate some python code to scala code. So I'm a total
I'm trying 'translate' the sample code in here , but i can't find equivalent
when trying to translate the confirmation message to Norwegian i get the following error:
I have some jquery code which I am trying to translate to YUI. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.