[kw-pm] Problem with hash

abez abez at abez.ca
Wed Jul 29 11:52:54 PDT 2009


This doesn't parse:

> for (my $cC=0$cC<=$#aA;$cC++) {

Space everything out, I think you're missing a semi-colon

for ( my $cC = 0 ; $cC <= $#aA ; $cC++ ) {

put use strict; up at the top. It'll tell you that vHash != vHsh
(misspelling).

Then add a:

use Data::Dumper;
print Dumper(\%vHsh);

at the end of your program and you'll see the hash it makes.
You'll notice you're checking for SS0 and S0 because you have this
"S".$item2 everywhere.

As well you're checking for $vHsh{$itm1}{$itm2}{"SYSTEM"} but in your
examples you never set "SYSTEM", you in fact set $vHsh{$itm1}{$itm2} to
a scalar value, not a hashref

"use strict;" will attempt to spell check your variables. I recommend
always using it.

I've attached my code at the end of this message.

abram


Robert Pike wrote:
> First off, thanks to all that supplied me with feedback in regards to the issue I was having with redirection in perl. 
> I loop through an array, split the values, and put certain values in a hash. Hopefully this will be enough info to get some feedback.
> 
> my %vHash;
> $vHsh{"101010"}{"S0"} = "text";
> $vHsh{"101010USED"}{"S0"} = "text";
> 
> my @aA = ("101010~S0", "101010USED~S0");
> #if I were to do this 
> for (my $cC=0$cC<=$#aA;$cC++) {
>     ($itm1, $itm2) = split(/\~/,$aA[$cC]);    
>     if (!exists($vHsh{$itm1}{"S".$itm2}{"SYSTEM"})) { #--- also tried defined
>         $vHsh{$itm1}{"S".$itm2}{"SYSTEM"} = "TEXT";
>     }
> }
> 
>     The if statement (with the defined/exists check) criteria passes first pass but fails in the second. Can anyone tell me why?
> 
> 
>       __________________________________________________________________
> Connect with friends from any web browser - no download required. Try the new Yahoo! Canada Messenger for the Web BETA at http://ca.messenger.yahoo.com/webmessengerpromo.php
> 
> _______________________________________________
> kw-pm mailing list
> kw-pm at pm.org
> http://mail.pm.org/mailman/listinfo/kw-pm

use strict;
use Data::Dumper;
{ #make a new scope
  # this version does not work properly
  # this is a minor modification to the original version
  my %vHsh;
  $vHsh{"101010"}{"S0"} = "text";
  $vHsh{"101010USED"}{"S0"} = "text";

  my @aA = ("101010~S0", "101010USED~S0");
  #if I were to do this
  my ($itm1, $itm2); #satisfy use strict
  for (my $cC=0;$cC<=$#aA;$cC++) {
      ($itm1, $itm2) = split(/\~/,$aA[$cC]);
      if (!exists($vHsh{$itm1}{"S".$itm2}{"SYSTEM"})) { #--- also tried
defined
 	 warn "Found [$itm1 $itm2]";
          $vHsh{$itm1}{"S".$itm2}{"SYSTEM"} = "TEXT";
      }
  }
  print Dumper(\%vHsh);
}

print "$/ Now again but correctly $/";
{
  # this is the new verison
  my %vHsh;
  $vHsh{"101010"}{"S0"}{"SYSTEM"} = "TEXT";
  $vHsh{"101010USED"}{"S0"}{"SYSTEM"} = "TEXT";

  my @aA = ("101010~S0", "101010USED~S0");
  #if I were to do this
  for ( my $cC = 0 ; $cC <= $#aA ; $cC++ ) {
      my ($itm1, $itm2) = split(/\~/,$aA[$cC]); # note the scope
      if (!exists($vHsh{$itm1}{$itm2}{"SYSTEM"})) { #--- also tried defined
 	 warn "Found [$itm1 $itm2]";
          $vHsh{$itm1}{$itm2}{"SYSTEM"} = "TEXT";
      }
  }
  print Dumper(\%vHsh);
}




-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://mail.pm.org/pipermail/kw-pm/attachments/20090729/3b8f9879/attachment.bin>


More information about the kw-pm mailing list