9

Does any 1 have any idea what is $; (first split function argument) in the following code snippet:

      local(@a) = ();
      local($i) = 0;
      for ($i = 0; $i < $d; $i++) {
         @a = split($;, @b[$i]);
         $c     = @a[0];
      }

The scalar is not found any where in the script other than in the for loop.

Any help is appreciated.

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Ag.
  • 91
  • 2
  • 2
    Wow, that must be some old code. – mob Feb 04 '10 at 17:31
  • Must be *really* old code -- `$;` was deprecated with the first release of Perl 5, IIRC. There's certainly no use for it after Perl 4. – friedo Feb 04 '10 at 17:38
  • 5
    no, it's not deprecated; perl4-style multi-dimenional arrays are still handy where you have very large, sparse datastructures and you only care about the leaves. – ysth Feb 04 '10 at 17:57
  • 1
    '$;' with 'local', non-list 'for' and '@a[0]' not '$a[0]' means old Perl 4 code. – Alexandr Ciornii Feb 05 '10 at 09:12
  • possible duplicate of http://stackoverflow.com/questions/2578671/where-can-i-find-information-about-perls-special-variables – Sinan Ünür Apr 05 '10 at 13:49

2 Answers2

13

Perl's special variables are documented in perlvar, including $;

$SUBSEP

$;

The subscript separator for multidimensional array emulation. If you refer to a hash element as

$foo{$a,$b,$c}

it really means

$foo{join($;, $a, $b, $c)}

But don't put

@foo{$a,$b,$c}  # a slice--note the @

which means

($foo{$a},$foo{$b},$foo{$c})

Default is "\034", the same as SUBSEP in awk. If your keys contain binary data there might not be any safe value for $;. (Mnemonic: comma (the syntactic subscript separator) is a semi-semicolon. Yeah, I know, it's pretty lame, but $, is already taken for something more important.)

Consider using "real" multidimensional arrays as described in perllol.

We could guess about why it's being used (e.g., maybe @b contains some hash's keys), but knowing how @b is created would let us provide more helpful answers.

Note also that @b[$i] and @a[0] should probably be

$b[$i]

and

$a[0]

instead. With the leading @, they're single-element array slices, but with $, they're simple scalars.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
0

The Perl special variables are listed in perlvar.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
sud03r
  • 19,109
  • 16
  • 77
  • 96
  • 3
    Why did you link to some random (and outdated) page when you could have linked to the official Perl documentation? – Ether Feb 04 '10 at 17:52
  • NB: brian d foy has edited the question and it now links to the official documentation. – Quentin Feb 04 '10 at 23:45