Re: Values of an array..

2003-07-21 Thread Eric Wong
On Mon, Jul 21, 2003 at 02:38:20PM -0700, Andrew Hurst wrote:
> At 01:22 PM 7/21/2003 -0800, Dennis Stout wrote:
> >> >"Dennis Stout"  wrote ...
> >> > my %user_list = get_users($where);
> >> >
> >> > foreach (keys %user_list) {
> >> > my $user = $_;
> >> > foreach (@{$user_list{$user}{DOMAIN}}) {
> >> > $user_list{$user}{DOMAINS} .=
> >> >"$user_list{$user}{DOMAIN}[$_],";
> >> > }
> >> > chop($user_list{$user}{DOMAINS});
> >> >...
> >>
> >> >$user_list{$user}{DOMAINS} .= "$user_list{$user}{DOMAIN}[$_],";
> >>
> >> That line is the culprit.  $_ contains each domain name as a text
> >> string.  Which evaluates to 0 when used as an integer, which then gives 
> >the
> >> first entry in the array.  Thus you get the first entry in the array as
> >> many times as you have entries in the array.  Try changing that to a
> >> regular (indexed) for loop and that should fix it.
> >
> >Good eye, I was too busy thinking of hashes and things.
> >
> >I hate array's.
> >
> >Should just be $user_list{$user}{DOMAINS} .= "$_,"; huh?
> 
> Yep.  After I sent the last email I realized that suggesting an indexed
> for loop was more effort than just deleting part of the string already
> there, like you just suggested.

This would be a good place for a join (perldoc -f join):

$user_list{$user}{DOMAINS} = join ',', @{$user_list{$user}{DOMAIN}};

which lets you do away with the trailing chop() as well.

Since this has nothing to do with mod_perl, if you must reply, please
do it off-list.

Eric


Re: Values of an array..

2003-07-21 Thread Andrew Hurst
At 01:22 PM 7/21/2003 -0800, Dennis Stout wrote:
> >"Dennis Stout"  wrote ...
> > my %user_list = get_users($where);
> >
> > foreach (keys %user_list) {
> > my $user = $_;
> > foreach (@{$user_list{$user}{DOMAIN}}) {
> > $user_list{$user}{DOMAINS} .=
> >"$user_list{$user}{DOMAIN}[$_],";
> > }
> > chop($user_list{$user}{DOMAINS});
> >...
>
> >$user_list{$user}{DOMAINS} .= "$user_list{$user}{DOMAIN}[$_],";
>
> That line is the culprit.  $_ contains each domain name as a text
> string.  Which evaluates to 0 when used as an integer, which then gives the
> first entry in the array.  Thus you get the first entry in the array as
> many times as you have entries in the array.  Try changing that to a
> regular (indexed) for loop and that should fix it.
Good eye, I was too busy thinking of hashes and things.

I hate array's.

Should just be $user_list{$user}{DOMAINS} .= "$_,"; huh?
Yep.  After I sent the last email I realized that suggesting an indexed for 
loop
was more effort than just deleting part of the string already there, like 
you just
suggested.

Thanks
No problem.

-Andrew


Dennis



Re: Values of an array..

2003-07-21 Thread Dennis Stout
> >"Dennis Stout"  wrote ...
> > my %user_list = get_users($where);
> >
> > foreach (keys %user_list) {
> > my $user = $_;
> > foreach (@{$user_list{$user}{DOMAIN}}) {
> > $user_list{$user}{DOMAINS} .=
> >"$user_list{$user}{DOMAIN}[$_],";
> > }
> > chop($user_list{$user}{DOMAINS});
> >...
>
> >$user_list{$user}{DOMAINS} .= "$user_list{$user}{DOMAIN}[$_],";
>
> That line is the culprit.  $_ contains each domain name as a text
> string.  Which evaluates to 0 when used as an integer, which then gives the
> first entry in the array.  Thus you get the first entry in the array as
> many times as you have entries in the array.  Try changing that to a
> regular (indexed) for loop and that should fix it.

Good eye, I was too busy thinking of hashes and things.

I hate array's.

Should just be $user_list{$user}{DOMAINS .= "$_,"; huh?

Thanks

Dennis



Re: Values of an array..

2003-07-21 Thread Dennis Stout
> shouldnt 'my $user' be outside the foreach loop?

No, it's supposed to be changed each iteration through the loop.  Basically it
saves $_ to something else, so the next foreach loop doesn't overwrite it,
since I need it's value as a key for the hash the next loop creates.

Dennis



Re: Values of an array..

2003-07-21 Thread Gedanken
shouldnt 'my $user' be outside the foreach loop?  

I had a similar coding problem this weekend - completely unrelated to 
mod_perl.  I was using 
www::mechanize (greatest module ever) to parse xml and generate files.  If 
i had 18 user sections, it 
was writing the last member to disk 18 times.  I changed the scope of my 
vars slightly and all worked just fine.

-- 
gedanken



Re: Values of an array..

2003-07-21 Thread Andrew Hurst


"Dennis Stout"  wrote ...
my %user_list = get_users($where);
foreach (keys %user_list) {
my $user = $_;
foreach (@{$user_list{$user}{DOMAIN}}) {
$user_list{$user}{DOMAINS} .=
"$user_list{$user}{DOMAIN}[$_],";
}
chop($user_list{$user}{DOMAINS});
...

$user_list{$user}{DOMAINS} .= "$user_list{$user}{DOMAIN}[$_],";
That line is the culprit.  $_ contains each domain name as a text 
string.  Which evaluates to 0 when used as an integer, which then gives the 
first entry in the array.  Thus you get the first entry in the array as 
many times as you have entries in the array.  Try changing that to a 
regular (indexed) for loop and that should fix it.

Hope that helps.
-Andrew