Thanks Chas but my req. is little bit different.

As I said the data in the array will not be fixed so I don't know how
many elements are present in the array. I don't want to just print the
contents of the array but to use the contents of the array.

For example if user select 3 option then my programme should pick up the
third element of the array 

Please help

Regards
Irfan.


-----Original Message-----
From: Chas Owens [mailto:[EMAIL PROTECTED] 
Sent: Thursday, August 16, 2007 4:16 PM
To: Sayed, Irfan (Irfan)
Cc: beginners@perl.org
Subject: Re: Array modification

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