Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: going throu...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 11 Topic 10982 of 12002
Post > Topic >>

Re: going through array of hash, it only goes through limited amount

by rich.japh@[EMAIL PROTECTED] (Richard Lee) Apr 23, 2008 at 09:37 PM

John W. Krahn wrote:
> Richard Lee wrote:
>>
>> something is wrong with this..
>>
>> say %yahoo's key contains the variable , X
>>
>> I wanted to go through the @[EMAIL PROTECTED]
 which has array of hashes... to see 
>> if one of the value is equal to
>> X and if it is, wanted to assign the key of the @[EMAIL PROTECTED]
 to $ex_var..
>>
>> Tracing the program, it only goes through 6 lines of keys in @[EMAIL PROTECTED]
 
>> (random keys) (it has 89 keys total).. what am i doing wrong?
>>
>>
>>
>>
>>      while (my ($keys,$values) = each(%yahoo) ) {
>>              no strict 'refs';
>>              MF: for my $i (0 .. $#t_array) {
>>                      for ( my($k,$v) = each(%{ $t_array[$i] } ) ) {
>
> The for loop is not doing what you appear to think it is supposed to 
> be doing:
>
> $ perl -le'
> my %hash = "A" .. "Z";
> for my $c ( 1 .. 3 ) {
>     my $i;
>     for ( my ( $k, $v ) = each %hash ) {
>         print "$c ", ++$i, qq[: \$_ = "$_"   \$k = "$k"   \$v = "$v"];
>         }
>     }
> '
> 1 1: $_ = "S"   $k = "S"   $v = "T"
> 1 2: $_ = "T"   $k = "S"   $v = "T"
> 2 1: $_ = "A"   $k = "A"   $v = "B"
> 2 2: $_ = "B"   $k = "A"   $v = "B"
> 3 1: $_ = "O"   $k = "O"   $v = "P"
> 3 2: $_ = "P"   $k = "O"   $v = "P"
>
>
> You need to use each() in a while loop instead.
>
>
>>                          my $keys_b = qr/$keys/;
>>                          if ( $v  =~ m/$keys_b/ ) {
>>                              $ex_var = $k;
>>                              last MF;
>>                      }
>>                   }
>>              }
>
>
> John

finally understood what you meant.. thanks!!

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

my %yahoo = qw(
        uno one
        dos two
);

my %color = qw(
        white  10
        yellow 20
        red   30
);

my @[EMAIL PROTECTED]
 = ( \%yahoo, \%color );

print Dumper(@[EMAIL PROTECTED]
);


for my $href ( @[EMAIL PROTECTED]
 ) {
    while ( my ($k, $v) = each %$href ) {
    #for my $keys ( keys %$href ) {
          print "\$k is $k\n";
          print "\$v is $v\n";
          print "==========\n";
    }
}


.././././././ref_each.pl
$VAR1 = {
          'uno' => 'one',
          'dos' => 'two'
        };
$VAR2 = {
          'white' => '10',
          'red' => '30',
          'yellow' => '20'
        };
$k is uno
$v is one
==========
$k is dos
$v is two
==========
$k is white
$v is 10
==========
$k is red
$v is 30
==========
$k is yellow
$v is 20
==========
 




 11 Posts in Topic:
wrote-only language?
peng.kyo@[EMAIL PROTECTED  2008-04-23 13:03:51 
Re: wrote-only language?
chas.owens@[EMAIL PROTECT  2008-04-23 12:46:41 
going through array of hash, it only goes through limited amount
rich.japh@[EMAIL PROTECTE  2008-04-23 13:23:50 
Re: going through array of hash, it only goes through limited am
krahnj@[EMAIL PROTECTED]   2008-04-23 11:16:39 
Re: going through array of hash, it only goes through limited am
rich.japh@[EMAIL PROTECTE  2008-04-23 14:55:31 
Re: going through array of hash, it only goes through limited am
rich.japh@[EMAIL PROTECTE  2008-04-23 15:04:45 
Re: going through array of hash, it only goes through limited am
rich.japh@[EMAIL PROTECTE  2008-04-23 21:21:06 
Re: going through array of hash, it only goes through limited am
rich.japh@[EMAIL PROTECTE  2008-04-23 21:37:41 
Re: wrote-only language?
rob.dixon@[EMAIL PROTECTE  2008-04-23 17:43:53 
Re: wrote-only language?
orasnita@[EMAIL PROTECTED  2008-04-23 20:27:45 
Re: wrote-only language?
Jenda@[EMAIL PROTECTED]   2008-04-24 00:50:49 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 10:18:34 CDT 2008.