49820

Do I need to reset a Perl hash index?

OK, this is a N00b question, but it's gotten me stumped:

I have the following Perl code:

%project_keys = ( cd => "continuous_delivery", cm => "customer_management", dem => "demand", dis => "dis", do => "devops", sel => "selection", seo => "seo" ); print "proj_code is $proj_code\n"; while ( ($key, $value) = each %project_keys ) { if ($key == $proj_code) { $url = "http://projects/".$project_keys{$key}."/setter"; last; } }

$proj_code is always passed in the same ('dis') and the print line shows that.

However, each time I run this, I'm getting a different value for project_keys{$key}.

What (no doubt obvious) thing am I doing wrong? I have seen comments about how each is 'fragile' - is that it?

Answer1:

If you already have the "key" you're expecting, check if it exists and use it...

Also, always use use strict; use warnings;

#!/usr/bin/perl use strict; use warnings; my $proj_code= 'dis'; my $url; my %project_keys = ( cd => "continuous_delivery", cm => "customer_management", dem => "demand", dis => "dis", do => "devops", sel => "selection", seo => "seo" ); $url = "http://projects/$project_keys{$proj_code}/setter" if exists $project_keys{$proj_code}; print "url: $url\n";

OUTPUTS:

url: http://projects/dis/setter

Answer2:

Yes, if you use each to iterate through a hash but don't iterate all the way till no result is returned, you will pick up the next time where you left off.

You can reset the internal iterator to the beginning by using keys or values before looping, like so:

keys(%project_keys); while ( ($key, $value) = each %project_keys ) {

That said, you have a number of other problems:

Use eq, not ==, to test for string equality.

Your whole loop is pointless; you don't need to iterate through a hash to look up a value; the loop can be replaced with simply doing:

if ( exists $project_keys{$proj_code} ) { $url = "http://projects/$project_keys{$proj_code}"; }

Answer3:

You're using each. Best not to. It's subtle and stores state within the hash.

Far better than

while( my ( $key, $value ) = each %hash ) { ... }

is to use a foreach loop on the keys function

foreach my $key ( keys %hash ) { my $value = $hash{$key}; ... }

Recommend

  • How do you create a simple Automation Extender for Visual Studio with UITypeEditor
  • How to merge 2 or more columns into one?
  • event not defined in Firefox
  • Post comment to WordPress Blog from iPhone programmatically
  • $this->params returns null in cakephp model
  • Simple regex for domain names
  • Edit assembly language code in Visual Studio while stepping through each statement
  • Serverless Framework Dynamo DB Table Resource Definition with Sort Key
  • Validity Method for Reference Classes
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • Using an enum contained in a Cloud Endpoint model on a Android client
  • Using Netbeans with Cygwin and SDL, including SDL.h creates strange error
  • What's wrong with my datatrigger binding?
  • Does Apportable support to build library binary (.a/.so)?
  • Cloud Code function running twice
  • Passing “get” parameters doesn't work, parameter not visible in the link
  • How to assign byte[] as a pointer in C#
  • Diff between two dataframes in pandas
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Use of this Javascript
  • How to access EntityManager inside Entity class in EJB3
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • VS2008 Enable C++ Exception with SEH
  • javascript inside java/jsp code
  • Perl system calls when running as another user using sudo
  • Why winpcap requires both .lib and .dll to run?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • JaxB to read class hierarchy
  • Recursive/Hierarchical Query Using Postgres
  • Reading document lines to the user (python)
  • UserPrincipal.Current returns apppool on IIS
  • Python/Django TangoWithDjango Models and Databases
  • git trying to push non-existent file … after clearing cache