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

The Archive Base Latest Questions

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

I can create an array with ’empty slots’ in it: $ perl -wde 1

  • 0

I can create an array with ’empty slots’ in it:

$ perl -wde 1
...
  DB<1> $x[2] = 0
  DB<2> x \@x
0  ARRAY(0x103d5768)
   0  empty slot
   1  empty slot
   2  0

or

  DB<3> $#y = 4
  DB<4> x \@y  
0  ARRAY(0x103d5718)
   0  empty slot
   1  empty slot
   2  empty slot
   3  empty slot
   4  empty slot

Please note: this is not the same as assigning undef.

But how do I specify that for an anonymous array using [ and ]?

This will not work:

  DB<5> x [,,0]
syntax error at (eval 27)[/usr/local/lib/perl5/5.10.0/perl5db.pl:638] line 2, near "[,"

And this fails too, since I only get the assigned value:

  DB<6> x []->[2] = 0
0  0

Bonus question: how can I check for an ’empty array slot’ in my Perl script?

Background: In my test scripts I would like to be able to compare array contents precisely. For example I want to distinguish between ‘not assigned’ and ‘assigned with an undef value’.

Thanks for any insights.

  • 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:16:08+00:00Added an answer on May 23, 2026 at 12:16 pm
    use feature qw/ say /;
    use strict;
    use warnings;
    
    my $aref;
    
    $#{$aref} = 4;
    $aref->[2] = undef;
    $aref->[3] = '';
    
    foreach my $idx ( 0 .. $#{$aref} ) {
        say "Testing $idx.";
        say "\t$idx exists." if exists $aref->[$idx];
        say "\t$idx defined." if defined $aref->[$idx];
    }
    
    OUTPUT:
    Testing 0.
    Testing 1.
    Testing 2.
        2 exists.
    Testing 3.
        3 exists.
        3 defined.
    Testing 4.
    

    We pre-allocated five spots in the anonymous array, @{$aref}. The top index is 4. We are able to find what the top index is the same way we created it; by testing the value of $#{$aref}. We can test for existence. We know everything between 0 and 4 was created. But Perl only reports “exists” for array elements that have specifically had something assigned to them (even if it’s undef). Therefore, $aref->[2] is reported to exist, but isn’t defined. Just for fun, we assigned '' to $aref->[3] to see a test report defined once. But the short story is that even though the array is pre-extended, we can still test for the difference between an element being initialized with undef, and an element being undef through array pre-extension, by using ‘exists‘.

    I can’t say that’s documented behavior of exists. So there’s no guarantee it wouldn’t change someday. But it works on 5.8, 5.10, 5.12, and 5.14.

    So, looking for a simple way to find which elements were initialized, which were defined, and which were not, here’s an example:

    use feature qw/ say /;
    use strict;
    use warnings;
    
    my $aref;
    
    $#{$aref} = 4;
    $aref->[2] = undef;
    $aref->[3] = '';
    
    my @initialized = grep { exists $aref->[$_] } 0 .. $#{$aref};
    my @defined = grep { defined $aref->[$_] } 0 .. $#{$aref};
    my @uninitialized = grep { not exists $aref->[$_] } 0 .. $#{$aref};
    my @init_undef = grep { exists $aref->[$_] and not defined $aref->[$_] } 0 .. $#{$aref};
    say "Top index is $#{$aref}.";
    say "These elements are initialized: @initialized.";
    say "These elements are not initialized: @uninitialized.";
    say "These elements were initialized with 'undef': @init_undef.";
    say "These elements are defined: @defined."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create an empty one-dimensional string array?
I can create an array and initialize it like this: int a[] = {10,
I know you can create an array like this: $a = array(); and append
How can I create an array of email addresses contained within a block of
In Postgresql you can create additional Aggregate Functions with CREATE AGGREGATE name(...); But this
this question can create a misunderstanding: I know I have to use CSS to
Is there anyway I can create a not in clause like I would have
Can c++ create array of pointers to different classes dynamically loaded from dll? ps.without
I can create an array of buttons in Windows Form but how can i
What would be the best way to create an array that can have an

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.