in reply to Looping through an array of hashrefs

The code you posted has syntax errors (missing parentheses around the if-condition, missing right curly brace). When I tried to fix them, all worked correctly:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; sub validate_properties { my $properties = shift; for my $prop (@{$properties}) { if (.5 < rand) { $prop->{supported} = 1; } else { $prop->{supported} = 0; } } return $properties } print Dumper validate_properties([map +{ id => $_ }, 1 .. 5]);

Output:

$VAR1 = [ { 'id' => 1, 'supported' => 0 }, { 'id' => 2, 'supported' => 0 }, { 'id' => 3, 'supported' => 0 }, { 'id' => 4, 'supported' => 1 }, { 'id' => 5, 'supported' => 0 } ];

So, please show the real code that exhibits the problem.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Looping through an array of hashrefs
by misterperl (Friar) on Sep 24, 2014 at 14:41 UTC
    # a simplified approach with less clutter you might find useful.. To me, reducing code I have to maintain 50% makes my job 1/2 as difficult.. You pass in an arrayref you didn't need any returned val I don't think....
    sub validate_properties { for ( @{$_[0]} ) { $_->{supported} = 0; $_->{supported}++ if .5 < rand; } }

      In that case, why not eliminate half the code in that loop?:

      sub validate_properties { for ( @{$_[0]} ) { $_->{supported} = .5 < rand; } }

      Or even inline it as: $_->{supported} = .5 < rand for @array;?

      Based on the (modified) original post, it should be $_->{supported} = isSupported($_) for @properties; rather than random.
      I don't see how saving the results of an isSupported() test could be described as "validating properties", but I suppose that's a lack of context speaking.

        nice tips thanks I can use that :) Voted u a ++...