rob_au has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -Tw use CGI; use XML::Simple; use strict; my $xs = XML::Simple->new; my $xml = eval { $xs->XMLin('../xml/users.xml') }; die $@ if ($@); my $cgi = CGI->new; my @foreach; foreach (@{%{$xml}->{'user'}}) { push (@foreach, $_) if $_->{'username'} eq $cgi->param('username') +; }; my @map = map { $_ if $_->{'username'} eq $cgi->param('username'); } @ +{%{$xml}->{'user'}};
To my mind, the foreach and map segments of code are equivalent - Herein lies the problem I suspect. They are not.
If indeed, these pieces of code are equivalent, one could rightly expect the output arrays of each code segment to be the same for a given input. This is not occurring - Instead, the array output from the map code segment comprises of a large number of null values (one for each non-matching field) and the single result expected (and returned from the foreach code segment).
And so my question is ... What am I doing wrong here? Is there a way to have map return no value (compared with null value) into the output array if a given condition on the input scalar is not met?
Ooohhh, Rob no beer function well without!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: foreach/map equivalency
by wog (Curate) on Nov 10, 2001 at 21:48 UTC | |
|
Re: foreach/map equivalency
by Masem (Monsignor) on Nov 10, 2001 at 21:50 UTC | |
|
Re: foreach/map equivalency
by ChemBoy (Priest) on Nov 10, 2001 at 21:53 UTC | |
|
Re: foreach/map equivalency
by gloryhack (Deacon) on Nov 10, 2001 at 21:48 UTC | |
by chromatic (Archbishop) on Nov 11, 2001 at 02:27 UTC | |
by gloryhack (Deacon) on Nov 11, 2001 at 04:07 UTC | |
by chromatic (Archbishop) on Nov 12, 2001 at 08:36 UTC | |
by gloryhack (Deacon) on Nov 12, 2001 at 10:20 UTC | |
|