in reply to I hate nested loops

Still nested loops, just the inner one is hidden:

@{ $netifaces->{ $_ } }{ @nets } = (1) x @nets for @eths;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: I hate nested loops
by jfroebe (Parson) on Aug 10, 2011 at 20:09 UTC

    The double foreach loop is a whole lot easier to read though ;-)

    Jason L. Froebe

    Blog, Tech Blog

      The double foreach loop is a whole lot easier to read though ;-)

      I don't know. Maybe for a newbie, but I don't find it so. I have no trouble reading slice syntax -- I use it a lot -- and find the way it extends from 1D to 2D and so on, very natural and readable.

      More importantly, I prefer the single line syntax to the multi-line because it makes it very clear that this is simply initialising the HoHs to a bunch of 1s, and nothing more. I like that I can read as "Initialise the 2D hash to 1s". There is little opportunity to misread the intent and screw it up by injecting extra stuff in the loops.

      It would be even nicer if I could do (something like):

      my %netifaces{ @eths }{ @nets } = (1) x *;

      My point I guess is that this initialisation (whilst possibly important), is essentially a trival part of the overall algorithm, and having it as a single line gives it a weight commensurate with that.

      Conversely, spreading it out over 5 lines with named iterators makes it take on a weight that makes it seem far more significant than it is in the overall scheme of things.

      Ie. This is but a single step in the algorithm, and having it as a single line in the source code makes that very clear. Which I like, and feel is helpful when reading the overall algorithm.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I prefer the nested loops, because that makes it easier to change the value that is assigned. For instance:
        for my $eth (@eths) { for my $net (@nets) { $netifaces{$eth}{$net} = func($eth, $net); } }
        The structure remains the same, regardless whether the RHS of the assignment is a literal, or a more complex expression.

        Or it can be easily extended to initialize more structures:

        for my $eth (@eths) { for my $net (@nets) { $netifaces{$eth}{$net} = func($eth, $net); $something_else{$eth} += $net; } }
        That was exactly the feeling I had that drove me to post. It is indeed a trivial part of the whole program.
Re^2: I hate nested loops
by rastoboy (Monk) on Aug 10, 2011 at 22:34 UTC
    That looks interesting, but I do not understand it. I don't suppose you'd care to insert some parentheses, or explain it?
      I don't suppose you'd care to insert some parentheses,

      I don't think adding unnecessary parens would help at all :)

      or explain it?

      Sure:

      @{ $netifaces->{ $_ } }{ @nets } = (1) x @nets for @eths; ^^^^^^^^^^ a hash reference @{ $netifaces->{ $_ } }{ @nets } = (1) x @nets for @eths; ^^^^^^^^^^^^^^^^^^ an element in that hash @{ $netifaces->{ $_ } }{ @nets } = (1) x @nets for @eths; iterated over ^^^ + @{ $netifaces->{ $_ } }{ @nets } = (1) x @nets for @eths; by these keys ^^^^^ @{ ... }{ @nets } ^^^^^^^^^^^^^^^^^^ a hash slice with the keys in @nets @{ ... }{ @nets } = (1) x @nets ...; is assigned ^ ^^^^^^^^^^^ a list of '1's the size of @nets

      Putting that all together, we get:

      For each value in @eths, create a key in $netifaces with a value that consists of a hash containing all of the keys in @nets, each with the value 1.

      Eg.

      @{ $netifaces->{ $_ } }{ @nets } = (1) x @nets for @eths;

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        lol awesome, thanks! makes sense--I think I need to be doing more slicing :-)