in reply to XMLin : Optimal Way to tell the difference between Array vs Scalar

I don't like that code. It should either push in both cases or assign in both cases. It also looks like a good candidate for the ternary operator, if ForceArray isn't an option.
@anArray = ref $xmlin->{$key} ? @{$xmlin->{$key}} : $xmlin->{$key};
  • Comment on Re: XMLin : Optimal Way to tell the difference between Array vs Scalar
  • Download Code

Replies are listed 'Best First'.
Re^2: XMLin : Optimal Way to tell the difference between Array vs Scalar
by JadeNB (Chaplain) on Aug 20, 2008 at 21:28 UTC
    @anArray = ref $xmlin->{$key} ? @{$xmlin->{$key}} : $xmlin->{$key};
    … which, since we're going through the keys in a hash, can be nicely combined with a map:
    @anArray = map { my $x; ref( $x = $xmlin->{$_} ) eq 'ARRAY' ? @$x : $x + } keys %$xmlin;
    (assuming that we want everything from the hash, not just a single entry—which is what I guess the poster wanted, which would explain the strange assignment/push choice).