2010-03-24 19 views
5

यह पहली बार है जब मैंने इस तरह से हैश और सरणी का उपयोग किया है - और यह काम कर रहा है। असल में, प्रत्येक कुंजी के लिए कई मान होते हैं जिन्हें मैं रिकॉर्ड करना चाहता हूं और फिर "key -> value -> value -> val ..."क्या यह एक पर्ल हैश बनाने का सही तरीका है जो सरणी का उपयोग करता है?

मेरा कोड निम्नानुसार है। मुझे आश्चर्य है कि यह काम करता है, इसलिए चिंतित है कि यह "गलती से" काम करता है। क्या यह इस कार्य को पूरा करने का सही तरीका है, या क्या कोई अधिक कुशल या उचित विधि है?

while ($source =~ m/(regex)/g) { #Get all key names from source 
    $listkey = $1; #Set current list key to the current regex result. 
    $list{$listkey} = ++$i unless $list{$listkey}; #Add the key to the hash unless it already exists. 
    $list{$listkey} = [] unless exists $list{$listkey}; #Add an array for the hash unless the hash already exists. 
    while ($loopcount==0) { 
      if ($ifcount==0) { 
        $listvalue=result_of_some_function_using_list_key; #Get the first list value by using the list key. 
        $ifcount++; #Increment so we only get the first list value once. 
      } else { 
        $listvalue=result_of_some_function_using_list_value; #Update the list value by using the last list value. 
      } 
      if ($listvalue) { #If the function returned a value... 
        push @{$list{$listkey}}, $listvalue; #...then add the value to the hash array for the key. 
      } else { #There are no more values and we need a new key. 
        $listkey=0; #Reset variable. 
        $listvalue=0; #Reset variable. 
        $loopcount++; #Increment loop counter to exit loop. 
      } 
    } 
$ifcount=0; #Reset count variable so the next listvalue can be generated from the new key. 
    $loopcount=0; #Reset count variable so another loop can begin for a new key. 
} 
foreach $listkey (keys %list) { #For each key in the hash. 
    print "$listkey --> "; #Print the key. 
    @values = @{$list{$listkey}}; #Reference the arrays of the hash. 
    print join ' --> ', @values; #Print the values. 
    print "\n"; #Print new line. 
} 
+7

आपको वास्तव में सख्त उपयोग में होना चाहिए; चेतावनियों का उपयोग करें; 'मोड – Zaid

+0

ओच, जिसने चेतावनियों की एक दुष्ट सूची बनाई। मुझे उनके माध्यम से जाना होगा। – Structure

+4

मुझे उम्मीद है कि वास्तविक कोड में कई टिप्पणियां नहीं हैं। –

उत्तर

2

निम्नलिखित कोड, अपने कोड के रूप में ही होता है अनावश्यक चरणों के बिना।

while ($source =~ m/(regex)/g) { # Get all key names from source 
    $listkey = $1;   # Grab current regex result. 
    $listvalue = result_of_some_function_using_list_key; 
    while ($listvalue) { 
     push @{$list{$listkey}}, $listvalue; 
     $listvalue = result_of_some_function_using_list_value; 
    } 
    $listkey = 0;    # Reset variable. 
    $domain = 0;     # Reset variable. 
} 

हालांकि, जैसा कि अन्य ने टिप्पणी की है, ज्यादातर मामलों में वैश्विक चर से बचा जाना चाहिए। इसके बजाए, सूची कुंजी और सूची मान को my() के साथ स्पष्ट रूप से स्कॉप्ड किया जाना चाहिए, और सूची मूल्यों को उत्पन्न करने के लिए फ़ंक्शंस में इनपुट के रूप में एक या अधिक पैरामीटर (डोमेन, सूची कुंजी और/या सूची मान) लेना चाहिए।

लाइनों

$list{$listkey} = ++$i unless $list{$listkey}; 
$list{$listkey} = [] unless exists $list{$listkey}; 

अपने मूल कोड में आवश्यकता नहीं होती, यह एक प्रवेश प्रारंभ करने में push @{ $list{$key} }, $value साथ पर्याप्त है।

+0

धन्यवाद, मुझे संक्षिप्त स्पष्टीकरण के बाद गलत कहां गया है, इसकी बेहतर समझ है। – Structure

1

नहीं! यदि यह काम करता है, तो यह निश्चित रूप से "गलती से" है। लेकिन यह भी स्पष्ट है कि यह आपका वास्तविक कोड नहीं है और आपने इसे "अनुवाद" में एक और उदाहरण में कई और गलतियां जोड़ दी हैं, इसलिए यह तय करना मुश्किल है कि इरादा क्या था, लेकिन आपके कार्यक्रम के कंकाल से जा रहा है, ऐसा लगता है जैसे यह कुछ होना चाहिए:

my %result; 

while ($source =~ m/(regex)/g) { 
    my $key = $1; 
    my $value = mangle($key); 
    while ($value) { 
    push @{ $results{$key} }, $value; 
    $value = frob($value); 
    } 
} 

और नहीं। हैश को शुरू करने के आपके प्रयास ऐसा नहीं कर रहे हैं जो आपको लगता है कि वे हैं (और आवश्यक नहीं हैं), लिखित के रूप में आपका लूप एक अच्छा विचार नहीं है, और न ही सभी वैश्विक चर हैं।

2

उपरोक्त कोड में कई अनावश्यक कदम हैं। पर्ल एक बहुत अर्थपूर्ण भाषा है, और इस तरह तर्क बहुत बस में व्यक्त किया जा सकता है:

# uncomment for some sample data 
# sub function {"@_" !~ /^\[{3}/ and "[@_]"} 
# my $source = 'one two three'; 

my %list; 
while ($source =~ m/(\S+)/g) { 
    my $key = $1; 
    my $value = function($key); 

    while ($value) { 
     push @{ $list{$key} }, $value; 
     $value = function($value) 
    } 
} 

for my $key (keys %list) { 
    print join(' --> ' => $key, @{$list{$key}}), "\n"; 
} 
+0

सहमत हुए। आम तौर पर, पर्ल में आपको केवल लूप इंडेक्स ('i (0 i = 0 ...' सी-स्टाइल लूप) या काउंटरों को उन मामलों में देखना चाहिए जहां आपको वास्तव में उन मानों के साथ कुछ करने की आवश्यकता होती है। काउंटर और इंडेक्स अक्सर भी होते हैं अजीब-से-स्पॉट बग का एक अच्छा स्रोत – plusplus

संबंधित मुद्दे

 संबंधित मुद्दे