Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Passing Hashes In and Out of Functions

by nickcave25 (Acolyte)
on Feb 20, 2000 at 23:41 UTC ( [id://3779]=perlquestion: print w/replies, xml ) Need Help??

nickcave25 has asked for the wisdom of the Perl Monks concerning the following question:

I have just discovered that it's not as simple a matter to swap entries from one hash to another as I had thought.

I am trying to assign an array of four hashes to another array of four hashes -- and it looks to me like that is a big "no-no". Here's an example:
#!/user/bin/perl -w use strict; my %linkTitle; my %linkCategory; my %linkBlurb; my %linkUrl; (%linkTitle,%linkCategory,%linkBlurb,%linkUrl) = AssignValues(); # apparenty this (or the other similarly commented statement or #both) is illegal and/or doesn't do what I think it should. # Print the assigned values: my $counter; for ($counter = 0; $counter < 3; $counter++) { print "linkTitle{$counter} is set to $linkTitle{$counter}"; print "linkCategory{$counter} is set to $linkCategory{$counter}"; + print "linkBlurb{$counter} is set to $linkBlurb{$counter}"; print "linkUrl{$counter} is set to $linkUrl{$counter}"; } sub AssignValues() { my %assignTitle; my %assignCategory; my %assignBlurb; my %assignUrl; $assignTitle{0} = "Yahoo!"; $assignCategory{0} = "Search Engines"; $assignBlurb{0} = "Perhaps you've heard of it."; $assignUrl{0} = "http://www.yahoo.com/"; $assignTitle{1} = "Slashdot"; $assignCategory{1} = "Tech News"; $assignBlurb{1} = "News for nerds. Stuff that matters."; $assignUrl{1} = "http://www.slashdot.org/";

Replies are listed 'Best First'.
Re: Passing Hashes In and Out of Functions
by btrott (Parson) on Feb 21, 2000 at 02:06 UTC
    Several things here:

    1) The reason this isn't working is because those hashes that you're returning are flattened into a big list, which is then assigned to the *first* hash in the list you're assigning to. The rest of the hashes get undefined. Read perlman:perlsub for more details.

    2) The second problem is that you probably don't want to do it like this, anyway. Investigate using an array of hash references to hold your data, something like this:

    @assign = ( { title => "Slashdot", category => "Tech News", blurb => "News for nerds. Stuff that matters.", url => "http://www.slashdot.org/" }, { title => "Perl Monks", category => "Perl", blurb => "Perl advice.", url => "http://www.perlmonks.org/" } );
    Now you can access your data like:
    for my $site (@assign) { print "Site:\n"; for my $field (keys %$site) { print "\t", $field, " = ", $site->{$field}, "\n"; } }
    Or, if you want a particular field of a particular site,
    print "Title: ", $assign[0]{title}, "\n";
    Read perlman:perlref or perlreftut for more details on using references.

    (And think about returning a reference from your subroutine rather than the actual hash/array.)

Re: Passing Hashes In and Out of Functions
by nickcave25 (Acolyte) on Feb 22, 2000 at 04:00 UTC
    Ahhh.... Yes, I looked in perlman:perlsub and I see what you mean about the flattened list thing!

    I like the array of has references idea... I will check it out.

    Thankya btrott.
Re: Passing Hashes In and Out of Functions
by nickcave25 (Acolyte) on Feb 20, 2000 at 23:45 UTC
    $assignTitle{1} = "Slashdot"; $assignCategory{1} = "Tech News"; $assignBlurb{1} = "News for nerds. Stuff that matters."; %assignUrl{1} = "http://www.slashdot.org/"; $assignTitle{2} = "Perl Monks"; $assignCategory{2} = "Perl"; $assignBlurb{2} = "Perl advice."; $assignUrl{2} = "http://www.perlmonks.org/"; return (%assignTitle,%assignCategory,%assignBlurb,%assignUrl); # apparently this (or the other similarly commented statement # or both) is illegal and/or doesn't do what I think it should. }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://3779]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found