Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

(RFC) Arrays: A Tutorial/Reference

by jdporter (Paladin)
on Jan 12, 2007 at 16:02 UTC ( [id://594413]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    @array = ( 1, 2, 3 );
    @array = function_generating_a_list();
    @array = @another_array;
    
  2. or download this
    @array = ( 'a', 'b', 'c' );
    
  3. or download this
    @array = ();
    
  4. or download this
    @array = 0;
    # and
    @array = ( 0 );
    
  5. or download this
    $count = @array;
    
  6. or download this
    print "# Elements: " . @array . "\n";
    
  7. or download this
    print "# Elements: ", scalar(@array), "\n";
    
  8. or download this
    $highest_index = $#array;
    
  9. or download this
    foreach ( 0 .. $#array ) {
      # $_ is set to each index number, in turn, from first (0) to last ($
    +#array)
    }
    
  10. or download this
    print "Here are your things: ", @array, "\n";
    
  11. or download this
    foreach ( @array ) { ...
    
  12. or download this
    $x = @array;
    @x = @array;
    
  13. or download this
    @array = ( 'a', 'b', 'c' );
    $x = pop @array;
    
  14. or download this
    push @array, 8, 10 .. 15;
    
  15. or download this
    @array = ( 'a', 'b', 'c' );
    $x = shift @array;
    
  16. or download this
    @array = ( 1, 2 );
    unshift @array, 'y', 'z';
    
  17. or download this
    $first_elem = $array[0];
    
  18. or download this
    $array[ $#array ] += 5;
    
  19. or download this
    ( $first, $third, $fifth ) = @array[0,2,4];
    
  20. or download this
    $n = @array[0..$#array];
    
  21. or download this
    @array[1..3] = ( 'x', 'y', 'z' );
    
  22. or download this
    $array[ -1 ]
    $array[ $#array ]
    
  23. or download this
    @array[ 0 .. $#array ]
    
  24. or download this
    @array[ 0 .. -1 ]
    
  25. or download this
    unshift @a, @b;
    # could be written as
    splice  @a, 0, 0, @b;
    
  26. or download this
    push @a, @b;
    # could be written as
    splice @a, $#a+1, 0, @b; # we have to index to a position PAST the end
    + of array!
    
  27. or download this
    $b = shift @a;
    # could be written as
    $b = splice  @a, 0, 1;
    
  28. or download this
    $b = pop @a;
    # could be written as
    $b = splice  @a, -1, 1;
    
  29. or download this
    @b = splice @a, 2, 3;
    
  30. or download this
    splice @a, 2, 0, @b;
    
  31. or download this
    splice @a,         # array to modify
        3,             # starting with 4th item
        2,             # remove (replace) two items
        'x', 'y', 'z'; # arbitrary list of new values to insert
    
  32. or download this
    @a = ();
    # could be written as
    splice @a, 0;
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://594413]
Approved by wfsp
Front-paged by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found