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

  • SEARCH
  • Home
  • 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 129875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:51:33+00:00 2026-05-11T05:51:33+00:00

The other day, needed to iterate over a subset of an array at a

  • 0

The other day, needed to iterate over a subset of an array at a time. Initially, I did this with splice – tearing apart the array was no problem in this case. It would return N elements at a time, or whatever was left at the end of the list. All went well.

Then it turned out I needed the array later. Instead of splice, I switched to array slices. BOOM! The program exploded, sending a stack overflow everywhere. What? Why? How? I played around with it, and found a couple variants that would work. Here’s the test script to demonstrate this problem:

use strict; use warnings;  my @array = qw(a b c d e f g h i j k l m n o p q r s t u v z x c v b a s d f g a s d f a se g); my $numPerTest = 5;  my $index = 0; print 'Separating out the subset before grepping it, good.\n'; while ($index < @array) {    print 'Iteration $index\n';    my @subset =  @array[$index..($index+$numPerTest)];    @subset = grep { defined $_ } @subset;    $index += $numPerTest; }  $index = 0; print 'Making a copy of the array before grepping works.\n'; while ($index < @array) {    print 'Iteration $index\n';    my @subset = grep { defined $_ } @{[ @array[$index..($index+$numPerTest)] ]};    $index += $numPerTest; }  $index = 0; print 'Grepping the array slice directly, explodey!\n'; while ($index < @array) {    print 'Iteration $index\n';    my @subset = grep { defined $_ } @array[$index..($index+$numPerTest)];    $index += $numPerTest; } 

(Actually, I just figured this one out, but I figured I might as well post it anyway. See if anyone else sees it. 🙂 )

(Also, if you don’t see it, this has another way of explaining why it happens.)

  • 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. 2026-05-11T05:51:34+00:00Added an answer on May 11, 2026 at 5:51 am

    By using the slice as an lvalue, you are enlarging the array every time it is too short. Hence, it will never be out of elements.

    In the first two examples, you only used it as an rvalue, thus no extra elements are created. In the third one it is an lvalue, and thus the elements are created, so that $_ can be assigned to.

    This is not a behavior specific to slices: in normal array access exactly the same behavior is shown.

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

Sidebar

Related Questions

I saw this the other day (scroll all the way down to see some
The other day I needed to archive a lot of data on our network
This question is related to one I asked the other day which I got
So the other day, I asked this question about how to combine three complex
The other day I set up an Ubuntu installation in a VM and went
The other day I decided to write an implementation of radix sort in Java.
The other day, I tweaked a script for a friend's World of Warcraft addon.
the other day a colleague of mine stated that using static classes can cause
I was thinking the other day on normalization, and it occurred to me, I
a colleague pointed me the other day to BCEL which , as best 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.