G'day Magnolia25,

Here's yet another way to do this.

#!/usr/bin/env perl use strict; use warnings; my $VAR1 = { ... as per your OP ... }; my (@all_states, @comment_states); for (map values %$_, values %$VAR1) { my ($state, $comment) = ('', 0); for (@$_) { $state = substr $_, 11 if 0 == index $_, 'state_name='; $comment = 1 if 0 == index $_, 'comments='; } push @all_states, $state if $state; push @comment_states, $state if $comment; } use Data::Dump; dd \@all_states; dd \@comment_states;

Output:

["Alaska", "Puerto Rico", "Washington", "West Virginia"] ["Washington", "West Virginia"]

That handles the words '"comments" is present'; however, your example output suggests you want '"comments" is absent'. Changing if to unless in the second push:

push @comment_states, $state unless $comment;

And the output becomes:

["West Virginia", "Washington", "Puerto Rico", "Alaska"] ["Puerto Rico", "Alaska"]

I'll leave you to decide which you actually want. :-)

— Ken


In reply to Re: Get all hash value into array by kcott
in thread Get all hash value into array by Magnolia25

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.