Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL array handling 2

Status
Not open for further replies.

santanudas

Technical User
Mar 18, 2002
121
GB
Hi guys,

How can I determine the position of an array element if the value is known? For example, if we have

my @sss = qw(Sunday Monday Tuesday Wednesday Thursday);

Then print $sss[3] will return “Wednesday”

I just need to do the other way round, i.e. if I know the element “Wednesday” then how can I return 4?

Thanks in advance. Cheers!!!
 
If it's just a one-off, use a loop:
Code:
my @sss = qw(Sunday Monday Tuesday Wednesday Thursday);
my $to_find = 'Wednesday';
my $position = -1;
for ( 0 .. $#sss ) {
   if ( $sss[$_] eq $to_find ) {
      $position = $_;
      last;
   }
}

If you want to do it regularly, you should use a hash to map each day with its number:
Code:
my @sss = qw(Sunday Monday Tuesday Wednesday Thursday);
my %days;
$days{$sss[$_]} = $_ for 0 .. $#sss;
my $to_find = 'Wednesday';

my $position = $days{ $to_find };
 
Hi Ishnid,

Thank you very much for the code – that’s exactly what I was looking for. You saved me a lot of time. Thank you very very much.

I think, I better ask you another thing here, though it’s nothing related to the original query. How can I encrypt the URI string (or maybe put some extra garbage in it) so that it’s not easily understandable using Perl/CGI?

Like, I’m passing a URI like this:
Code:
<a herf=”/cgi-bin/xyz/xyz.cgi?MODE=ABC&DIR=$blah&FILE=$blah.txt”>

and on the next page this is exactly what we gonna see in the address bar. How to make it a bit meaning less to the viewers? Any idea?

Thank you once again, Cheers!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top