Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
A lesser known fact is that you can delete array cells too.. The aspirin in this case is to think of arrays as arrays of pointers to scalars. If the pointer is nothing then the array cell doesn't exist. If the pointer leads to a scalar then you have a cell in which to store values. I think this is how arrays are implemented internally...
#!/usr/bin/perl use strict; use warnings; no warnings 'uninitialized'; my @array = 1 .. 5; $\ = "\n"; sub check { print "array length: " . @array; use tt; [% FOR check IN ["exists", "defined", "true", "" ] %] for my $i ( 0 .. $#array ) { print "[% check %]([$i]) is " . [% check %]( $array[$i] ); } [% END %] no tt; print; } sub true { !!$_[0] } check; delete $array[$_] for 2 .. 3; # creates a hole check; undef $array[0]; # operates on the slot, not the array check; delete $array[-1]; # reduces length check; undef @array; # clears the array check; @array = 1 .. 3; check;
This yields:
array length: 5 exists([0]) is 1 exists([1]) is 1 exists([2]) is 1 exists([3]) is 1 exists([4]) is 1 defined([0]) is 1 defined([1]) is 1 defined([2]) is 1 defined([3]) is 1 defined([4]) is 1 true([0]) is 1 true([1]) is 1 true([2]) is 1 true([3]) is 1 true([4]) is 1 ([0]) is 1 ([1]) is 2 ([2]) is 3 ([3]) is 4 ([4]) is 5 array length: 5 exists([0]) is 1 exists([1]) is 1 exists([2]) is exists([3]) is exists([4]) is 1 defined([0]) is 1 defined([1]) is 1 defined([2]) is defined([3]) is defined([4]) is 1 true([0]) is 1 true([1]) is 1 true([2]) is true([3]) is true([4]) is 1 ([0]) is 1 ([1]) is 2 ([2]) is ([3]) is ([4]) is 5 array length: 5 exists([0]) is 1 exists([1]) is 1 exists([2]) is 1 exists([3]) is 1 exists([4]) is 1 defined([0]) is defined([1]) is 1 defined([2]) is defined([3]) is defined([4]) is 1 true([0]) is true([1]) is 1 true([2]) is true([3]) is true([4]) is 1 ([0]) is ([1]) is 2 ([2]) is ([3]) is ([4]) is 5 array length: 4 exists([0]) is 1 exists([1]) is 1 exists([2]) is 1 exists([3]) is 1 defined([0]) is defined([1]) is 1 defined([2]) is defined([3]) is true([0]) is true([1]) is 1 true([2]) is true([3]) is ([0]) is ([1]) is 2 ([2]) is ([3]) is array length: 0 array length: 3 exists([0]) is 1 exists([1]) is 1 exists([2]) is 1 defined([0]) is 1 defined([1]) is 1 defined([2]) is 1 true([0]) is 1 true([1]) is 1 true([2]) is 1 ([0]) is 1 ([1]) is 2 ([2]) is 3
-nuffin
zz zZ Z Z #!perl

In reply to Re: "A meditation on Hashes", or "Why i need more aspirin" by nothingmuch
in thread "A meditation on Hashes", or "Why i need more aspirin" by EvanK

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found