www.webdeveloper.com
Results 1 to 4 of 4

Thread: Parse Array Into Hash

  1. #1
    Join Date
    May 2004
    Location
    Manchester, UK
    Posts
    116

    Parse Array Into Hash

    Hi everyone!

    I have a form with various check boxes on it - each named something like:

    Code:
    community_change_setting_NOTIFY_BLOG_1_POST
    community_change_setting_NOTIFY_BLOG_1_COMMENT
    community_change_setting_NOTIFY_BLOG_2_POST
    community_change_setting_NOTIFY_BLOG_2_COMMENT
    community_change_setting_NOTIFY_POST
    community_change_setting_NOTIFY_COMMENT
    The values are stored in a hash %Q. I am trying to format these into the following hash:

    Code:
      my $settings = {
                       NOTIFY => {
                                   BLOG => {
                                             1 => {
                                                    POST => $Q{whatever},
                                                    COMMENT => $Q{whatever},
                                                  },
                                             2 => {
                                                    POST => $Q{whatever},
                                                    COMMENT => $Q{whatever},
                                                  },
                                           },
                                   POST => $Q{whatever},
                                   COMMENT => $Q{whatever},
                                 },
                      };
    Any thoughts? At the moment my best effort is the following but it obviously doesn't work

    Code:
      my $settings;
      foreach my $k (sort keys %Q)
      { if ($k =~ m/^community_change_settings_(.*)$/)
        { my @keys = split('_', uc $1);
          my $set = $settings;
          foreach my $k (@keys)
          { $set = $set->{$k};
          }
          $set = $Q{$k};
        }
      }
    Thanks
    Jay

  2. #2
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Is this the structure you are talking about?
    Code:
    my %Q=("community_change_setting_NOTIFY_BLOG_1_POST" => "post1", "community_change_setting_NOTIFY_BLOG_1_COMMENT" => "comment1", "community_change_setting_NOTIFY_BLOG_2_POST" => "post2", "community_change_setting_NOTIFY_BLOG_2_COMMENT" => "comment2",  "community_change_setting_NOTIFY_POST" => "post", "community_change_setting_NOTIFY_COMMENT" => "comment");
      my $settings = {
                       NOTIFY => {
                                   BLOG => {
                                             1 => {
                                                    POST => $Q{community_change_setting_NOTIFY_BLOG_1_POST},
                                                    COMMENT => $Q{community_change_setting_NOTIFY_BLOG_1_COMMENT},
                                                  },
                                             2 => {
                                                    POST => $Q{community_change_setting_NOTIFY_BLOG_2_POST},
                                                    COMMENT => $Q{community_change_setting_NOTIFY_BLOG_2_COMMENT},
                                                  },
                                           },
                                   POST => $Q{community_change_setting_NOTIFY_POST},
                                   COMMENT => $Q{community_change_setting_NOTIFY_COMMENT},
                                 },
                      };

  3. #3
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Give this a try:
    Code:
    #!/usr/bin/perl -w
    use warnings;
    use strict;
    use CGI qw(:standard);
    use CGI::Carp qw(fatalsToBrowser);
    use Data::Dumper;
    print header;
    my %Q=("community_change_setting_NOTIFY_BLOG_1_POST" => "post1", "community_change_setting_NOTIFY_BLOG_1_COMMENT" => "comment1", "community_change_setting_NOTIFY_BLOG_2_POST" => "post2", "community_change_setting_NOTIFY_BLOG_2_COMMENT" => "comment2",  "community_change_setting_NOTIFY_POST" => "post", "community_change_setting_NOTIFY_COMMENT" => "comment");
    
      my ($settings,@keys,$set,$k1);
      foreach my $k (sort keys %Q)
      { if ($k =~ m/^community_change_setting_(.*)$/)
        {
         @keys = split('_', uc $1);
         eval '$settings->{'.join('}->{',@keys).'}=$Q{$k};';
        }
      }
    
    print '<pre>',Dumper $settings,'</pre>';
    exit;
    Too bad it uses 'eval', but whatever works.
    Last edited by Ultimater; 02-21-2006 at 07:48 PM.

  4. #4
    Join Date
    May 2004
    Location
    Manchester, UK
    Posts
    116
    Cheers Ultimater - yeah I think eval is the only way I'm gonna be able to do this? Seems strange that there isn't a better way...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles