On 8/16/07, Sayed, Irfan (Irfan) <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I have one array which stores some data after executing specific
> command. Depends on situation , command has different output at
> different time. sometime array may store 4 values or it may store 5
> values.
>
> Now my req. is that I need to assign no. to those values.
>
> for example:
>
> if array is @array1=(data1,data2,data3,data4);
>
> now i need to assign 4 no. as there are four elements are present in
> this array. So my array should look like this.
>
> @array1=(1 data1 2 data2 3 data3 4 data4);
>
> basically, i wanted to do indexing or to create hash. so that no. 1 will
> get assigned to first element and 2 will get assigned to sec. element
> and so on......
>
> Can somebody please help.
>
> Regards
> Irfan.

Well, you could always say

my @a = qw<data1 data2 data3 data4 data5>;
my $key = 1;
my %h = map { $key++ => $_ } @a;

But why would you want to?  data1 is index 0, data2 is index 1, etc.
Just adjust your numbers by one.  For instance you could print out the
contents of @a above prepended with 1 .. 5 like this

my $i = 1;
for my $item (@a) {
    print "$i $tem";
    $i++;
}

You could then read a number from the user

my $input = <>;

and then print out the corresponding item

print $a[$input - 1];

There is very little gained by using a hash in this situation.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to