HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

how to access elemt in array from perl

 
SOLVED
Go to solution
diwakar_4
Frequent Advisor

how to access elemt in array from perl

Hi All,


I have one array say:

a=('jan','feb','mar','apr','may','june','jul') ,[(2,4)];

i am not able to compile thsi statment. It gives me the error like:

Can't modify constant item in scalar assignment at af.pl line 2, near "),"
Execution of af.pl aborted due to compilation errors.


And also what is the difference betwenn [] and [()] operatores in accessing the elements.

Thanks
5 REPLIES
Steven E. Protter
Exalted Contributor

Re: how to access elemt in array from perl

Shalom,

A tutorial:
http://www.comp.leeds.ac.uk/Perl/associative.html

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor
Solution

Re: how to access elemt in array from perl

Hi:

It's not clear exactly what you are trying to do. You need to specify and array with the '@' sigil. Then it appears that you want an array slice of elements 2 and 4. Something like this:

#!/usr/bin/perl
use strict;
use warnings;
my @a=('jan','feb','mar','apr','may','june','jul');
print join ' ', @a[(2,4)], "\n";
my @b=qw( jan feb mar apr may june jul);
print join ' ', @b[2,4], "\n";

...You should use the 'strict' and 'warnings' pragmas as they will expose many errors.

You can drop the inner parentheses from the slice as I show in the second example.

@ SEP: You say you know Perl. You should know then that this was a simple array and not a hash.

Regards!

...JRF...
diwakar_4
Frequent Advisor

Re: how to access elemt in array from perl

Hi JRF,

Many thanks

Both the outputs are same. It means
@b[2,4]=@b[(2,4)]

If both are same then why () is used, is any specify significance of this?

Thanks
James R. Ferguson
Acclaimed Contributor

Re: how to access elemt in array from perl

Hi (again):

I don't know of any reason why the parenthesis would have been used. There's nothing to evalulate before the slice is taken.

Regards!

...JRF...
diwakar_4
Frequent Advisor

Re: how to access elemt in array from perl

Hi James,

Thanks a lot. That is fine for me.

Thanks
Diwakar