3

I saw this in a piece of code:

if (some_condition) {  
 $index++;  
}  
$index{$some_variable} = $index{$some_variable} + 1; 

What does $index{$some_variable} mean? And why is it used?

Thank you.

EDIT: index is defined as $index=0;

infinitloop
  • 2,863
  • 7
  • 38
  • 55

5 Answers5

10

If this code is written correctly, you will have these lines above it:

use strict;
use warnings;
my $index;
my %index;

if (some_condition) {  
    $index++;  
}  
$index{$some_variable} = $index{$some_variable} + 1; 

$index{$some_variable} is referring to a hash, and $index to a scalar. In perl, this is perfectly valid, and %index and $index will be considered two different variables.

This is also a reason why it is so important to use strict. Why use strict and warnings?

Community
  • 1
  • 1
TLP
  • 66,756
  • 10
  • 92
  • 149
  • 3
    It's also the reason why naming variables correctly is important for readability. `%index` should have been named `%indices` or something more meaningful. – DVK Nov 14 '11 at 19:51
  • 1
    @DVK : I'm not too sure about the plural form. All too often I find myself writing stuff like `for my $lookup ( keys %indices ) { ... say $indices{$lookup}; }` and wondering whether it would be better to stick with `%index` instead. – Zaid Nov 14 '11 at 20:02
  • 1
    @Zaid: then just go the Japanese way and use `index_group` or sth :) (Japanese doesn't have true plurals, many times "plurals" are formed by adding "tachi" (group) to a word.) – ninjalj Nov 14 '11 at 20:08
  • 2
    @DVK For what it's worth, Damian (PBP) suggests hashes be named in the singular since it is one element that is normally accessed at a time. – JRFerguson Nov 14 '11 at 20:08
  • 1
    @DVK Exactly. If the author had been naming variables appropriately, this question would never have been asked. Case in point. – TLP Nov 14 '11 at 20:10
  • 3
    @Zaid, In my opinion, hashes that contains a collection should be plural (%fields), and hashes that represent one object (%record) should be singular – ikegami Nov 14 '11 at 20:48
  • @ikegami - brilliant summation. That should go into every style guide/best practices. – DVK Nov 14 '11 at 20:55
9

It's retrieving an entry in the %index hash using a key whose value is the value of $some_variable

(Note: There may also exist a scalar named $index but it will occupy a separate namespace. That is, you can have both a hash and a scalar named index and they will not conflict.)

Wayne
  • 59,728
  • 15
  • 131
  • 126
  • but index is defined like $index = 0; – infinitloop Nov 14 '11 at 19:27
  • 5
    But there will be a `%index` assignment also, `$index` is a scalar, `%index` is a hash, and they're two different variables. When accessing a entry in the hash that is scalar, you use `$index{$myentry}`. – Jonathan M Nov 14 '11 at 19:28
6

Perl has several namespaces

  • $var is a scalar variable
  • @var is an array variable, and $var[$i] is an element of that array.
  • %var is a hash variable, and $var{$i} is an element of that hash.
ninjalj
  • 42,493
  • 9
  • 106
  • 148
5

The $index in the $index++; statement is a scalar. It has nothing to do with the $index{$some_variable} statement that follows it.

The $index{$some_variable} is part of a hash, %index. Hashes (or associative arrays) consist one or more pairs, each pair consisting of a key and a value. The key is used to access the value.:

my %hash = ( key_A => value_A,    # Here $hash{key_A} accesses 'value_A'
             key_B => value_B,    # ... $hash{key_B} gives 'value_B'
             key_Z => value_Z  ); # 'value_Z' returned by $hash{key_Z}

Analyzing $index{$some_variable} = $index{$some_variable} + 1;, the value of $index{$some_variable} is accessed, incremented by one and reassigned to the same key.

See perldoc perlintro for a gentle introduction to variable types in Perl, and perldoc perldsc for more complex data structures.

Zaid
  • 36,680
  • 16
  • 86
  • 155
2

You might find perldata helpful.

JRFerguson
  • 7,426
  • 2
  • 32
  • 36