in reply to Re: Search string within an predefined array
in thread Search string within an predefined array

Hi Moritz, Can you help me understand what the lines

@d{@defstream} = (1) x @defstream;
and
if ($d{$dfs})
mean.
  • Comment on Re^2: Search string within an predefined array

Replies are listed 'Best First'.
Re^3: Search string within an predefined array
by toolic (Bishop) on Jan 16, 2009 at 18:20 UTC
    I'm not moritz, but I'll give it a shot...
    @d{@defstream} = (1) x @defstream;
    This is a way to initialize the %d hash variable. Each element of the @defstream array is used as a hash key, and all values of the hash will equal 1. @d{@defstream} is a hash slice (see Subscripts). Also see Multiplicative Operators for an explanation of "x". It is equivalent to the more verbose:
    foreach my $dfs (@defstream) { $d{$dfs} = 1; }
    if ($d{$dfs})
    The "if" tests if the expression in the parentheses, which is a hash subscript, is true. I think moritz probably meant to use:
    if ($d{$stream}) {
    This will test whether the %d hash contains a key whose name is given by the value of the $stream variable. As hbm points out, it may be more appropriate to use exists.