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 1081285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:08:59+00:00 2026-05-16T22:08:59+00:00

I was noticing some curious behavior with Perl’s split command, particularly in cases when

  • 0

I was noticing some curious behavior with Perl’s split command, particularly in cases when I would expect the resulting array to contain empty strings ”, but it actually doesn’t.

For example, if I have a delimiter(s) at the end (or the beginning) of the string , the resulting array does not have an empty string(s) ” as the last (or first) element.

Example:

@s = split(/x/, 'axb')

produces 2 element array [‘a’,’b’]

@s = split(/x/, 'axbx')

produces same array

@s = split(/x/, 'axbxxxx')

produces same array

But as soon as I put something at the end, all those empty strings do appear as elements:

@s = split(/x/, 'axbxxxxc')

produces a 6 element array [‘a’,’b’,”,”,”,’c’]

Behavior is similar if the delimiters are at the beginning.

I would expect empty text between, before, or after delimiters to always produce elements in the split. Can anyone explain to me why the split behaves like this in Perl? I just tried the same thing in Python and it worked as expected.

Note: Perl v5.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-16T22:08:59+00:00Added an answer on May 16, 2026 at 10:08 pm

    From the documentation:

    By default, empty leading fields are preserved, and empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.)

    That explains the behavior you’re seeing with trailing fields. This generally makes sense, since people are often very careless about trailing whitespace, for example. However, you can get the trailing blank fields if you want:

    split /PATTERN/,EXPR,LIMIT

    If LIMIT is negative, it is treated as if an arbitrarily large LIMIT had been specified.

    So to get all trailing empty fields:

    @s = split(/x/, 'axbxxxxc', -1);
    

    (I’m assuming you made a careless mistake when looking at leading empty fields – they definitely are preserved. Try split(/x/, 'xaxbxxxx'). The result has size 3.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am noticing some weird behavior in my script, not sure why its occurring
I'm noticing some problems with the perlio layer in perl. Took me a good
I'm noticing some behavior that I don't like, and I'm wondering if this is
I'm noticing some strange behavior when I add a ComboBox to my Windows Form
I'm working on a media streaming app, and noticing some odd behavior. Media stops
Hi I'm noticing some odd behavior while using SMO and was wondering if anyone
Hi I'm noticing some odd behavior with the following code snippet function test {
I'm noticing some strange results with date conversions between MYSQL and PHP. Please see
I have a work around for this problem, but I've been noticing some peculiar
I started noticing strange behavior when navigating the main toolbar of my Winforms application,

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.