Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

It is not really clear what you are trying to achieve here. Your variable naming convention really does not make it clear what the desired output is.
You do some undefined check on $user_name and put the results in $no_name which you do not use. You push the city value into an array but try to print as a scalar, perhaps a hash of arrays is not the appropriate data structure to use.

Here is a slightly modified version of your code that uses a check_name sub and correctly displays the array contents. Hopefully this gives you a clue as to how to modify your code to achieve your goal.

use strict; use warnings; my $result = data(); foreach my $key ( keys %$result ) { my $value = join(',', @{$result->{$key}}); print "User Name = $key - City: $value\n"; } sub data { #db code stuff my $sql = [ { username => 'abc', city=> 'foo' }, { username => 'def', city=> 'bar' }, { username => 'ghi', city=> 'goo' }, { username => 'jkl', city=> 'car' }, { username => 'm-n-o', city=> 'car' }, ]; #... my %no_name_found; if ( @$sql ) { for ( @$sql ) { my $user_name = $_->{'username'}; my $city = $_->{'city'} || ''; if ( check_name( $user_name ) ) { push @{ $no_name_found{ $user_name } }, $city; } } } return \%no_name_found; } sub check_name { my $name = shift; if ( $name =~ /^\w+$/ ) { return $name; } } Output User Name = def - City: bar,bas User Name = abc - City: foo User Name = jkl - City: car User Name = ghi - City: goo

If you still are having problems please provide a more detailed description of what you are trying to achieve, include all relevant subs and a sample of input data and how you want it to display on output.


In reply to Re^2: Returning a hash and looping on the results. by rnewsham
in thread Returning a hash and looping on the results. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-18 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found