Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6645289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:16:07+00:00 2026-05-26T00:16:07+00:00

Trying to use map and grep to figure this out, any idea whats wrong?

  • 0

Trying to use map and grep to figure this out, any idea whats wrong? I keep getting a
Can’t use string (“10”) as a HASH ref while “strict refs” error when I try to print the values of the new hash

sub scrub_hash{
    my($self,$hash_ref) = @_;
    my $scrubbed_hash = map { defined $hash_ref->{$_} ? ($_ => $hash_ref->{$_}) : () } keys %{$hash_ref};   
    print STDERR "[scrub]". $_."\n"  for values %{$scrubbed_hash};
}   

used here
…

my $params_hash = $cgi->Vars();
my $scrubbed = $self->scrub_empty_params($params_hash) if $self->is_hash($params_hash);

in this case the params that are undefined when a form is submitted via post still show up as key1=&key2= so scrub takes em off

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:16:08+00:00Added an answer on May 26, 2026 at 12:16 am

    In this line here:

    my $scrubbed_hash = map { defined $hash_ref->{$_} ? ($_ => $hash_ref->{$_}) : () } keys %{$hash_ref};   
    

    You are assigning a list from map into the scalar $scrubbed_hash. map in scalar context will return the number of elements in the list (10).

    Rather than assigning to a scalar, assign to a plural hash:

    sub scrub_hash{
        my($self,$hash_ref) = @_;
        my %scrubbed_hash = map { defined $hash_ref->{$_} ? ($_ => $hash_ref->{$_}) : () } keys %{$hash_ref};   
        print STDERR "[scrub]". $_."\n"  for values %scrubbed_hash;
    }   
    

    If you really wanted to use a scalar for $scrubbed_hash you will need to wrap the map statement with {map {...} args} which will construct an anonymous hash out of the list.

    To filter out the elements in place, you could use the delete function:

    my %hash = (foo => 1, bar => undef, baz => 2);
    
    defined $hash{$_} or delete $hash{$_} for keys %hash;
    
    print join ', ' => keys %hash;  # foo, baz
    

    per the update:

    The scrub_empty_params method should look something like this:

    sub scrub_empty_params {
        my ($self, $hash) = @_;
        {map {defined $$hash{$_} ? ($_ => $$hash{$_}) : ()} keys %$hash}
    }
    

    If that is not working for you, then it may be that your values are defined, but have a length of 0.

    sub scrub_empty_params {
        my ($self, $hash) = @_;
        {map {(defined $$hash{$_} and length $$hash{$_}) ? ($_ => $$hash{$_}) : ()} keys %$hash}
    }
    

    You might want to remove a bit of boiler plate from your API by creating a different ->Vars() method that returns a filtered hash:

    sub clean_vars {
        my ($self) = @_;
        $self->scrub_empty_params($self->Vars)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn to use Map with Jaxb. I did this: @XmlElementWrapper(name =
I am getting a weird error when trying to use a Java map in
I'm trying to figure out how to best use MongoDB for a very small
I am trying to use a HashMap to map a unique string to a
I am trying to use the Google Map API- I've successfully managed to add
I am trying to use Lambda Expressions in a project to map to a
I am trying to use the automapping feature of Fluent with nHinbernate to map
background: trying to use the twitter gem for ruby-on-rails. in routes: map.resources :twitter_sessions map.finalize_twitter_sessions
I'm trying to use the values within a Map collection (doesn't matter what kind,
I am trying to make an app which use displacement map for 3d effect.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.