views:

116

answers:

5

This is not generic question. I have asked this question because I'm confused with creating Perl arrays.

  1. How do I create or define the Array or hash? What are other ways to do that?
  2. How do I clear a array or hash? What are other ways to do that?
  3. How do I create a array or hash with empty element?
  4. What are the ways to create hash with out values?
+5  A: 

If you're having trouble with basics like this, you should go and have a look at perldoc perlsyn. It covers language syntax and should tell you what you need.

Daenyth
+1  A: 

You can create arrays and hashes by assigning lists (including the empty list):

 my @a1; # empty array
 my @a2 = 'a';

 mh %h1; # empty hash
 my %h2 = 'a'; # a => undef
 my %h3 = ('a', 'b'); # 'a' => 'b'
 my %h4 = (a => 'b'); # 'a' => 'b'

You can clear an array or a hash by explicitly assigning an empty list:

 @a2 = ();
 %h3 = ();
Sinan Ünür
A: 

It sounds like you could benefit from reading tutorials and beginners guides to Perl. There are numerous examples of doing exactly what you ask.

One thing to keep in mind is that hashes and arrays are pretty much the same (conceptually) but that arrays are indexed by integers and hashes are indexed by strings. Syntactically you reference those indices differently, but conceptually that's how you can think of them.

CanSpice
I would add one other difference: arrays are ordered and hashes are unordered. That is, if you iterate over a given array, you will always get the items back in the same order, but a hash does not make an guarantee about the order of the values returned by [`keys`](http://perldoc.perl.org/functions/keys.html), [`values`](http://perldoc.perl.org/functions/values.html), or [`each`](http://perldoc.perl.org/functions/each.html) for hashes.
Chas. Owens
Yes, that's an excellent point. There are (of course) myriad other differences as well, but those two are probably the take-away for someone who's trying to get a handle on the major differences.
CanSpice
A: 

Create array:

my @array = (1, 2, 3, 4);

Create hash:

my %hash = (
    one   => 1,
    two   => 2,
    three => 2,
    four  => 2,
);

Clear array or hash:

@array = ();
%hash = ();

Create empty array / hash:

my @array = ();
my %hash = ();
kemp
+2  A: 

Lexical arrays and hashes are created empty. You can create a new lexical array or hash with my:

my @array;
my %hash;

For the most part, you should only be using lexical arrays and hashes, but you can create package arrays and hashes with our:

our @array;
our %hash;

@array and %hash may or may not have data in them in this case (if they were previously created, this will not clear their contents). The names of these variables are lexically scoped, but the data is package scoped, so if you say:

{
    our @a = (1 .. 5);
}
{
    our @a;
    print "@a\n";
}

It will still print "1 2 3 4 5\n".

There are many ways to clear a hash or array. The most common is to assign an empty list to it:

@array = ();
%hash  = ();

You can also use undef to clear a hash or array:

undef @array;
undef %hash;

You could also pop, shift, or splice items off of an array:

pop   @array while @array;
shift @array while @array;

splice @array, 0, scalar @array;

You can also modify the number of elements in and an array by assigning a number to the $#array form of its name. If you assign a negative value, the array is emptied:

$#array = -1;
Chas. Owens
+1: you are a very patient person...
carrot-top
@carrot-top What a nice way to describe my condition.
Chas. Owens