Return a hash from a sub routine

Discussion in 'Perl Misc' started by matg, Jan 10, 2006.

  1. matg

    matg Guest

    I'm sure this is a basic Perl question but I'm a newbie and can't seem
    to get my head around it.

    I want to pass 3 parameters eg. ("WM","windows","30/12/2005 00:00:00")
    to a subroutine that will open a file and return an associative array
    to the calling procedure - I believe the issue may lie in the way I
    call the sub - any clues? The sub executes correctly and does create
    the array but I can't seem to read anything from it once it is returned
    (i.e. the foreach loop produces nothing)

    Abridged code follows

    @Group = "WM";
    @Env = "windows";
    @Timekey = "30/12/2005 00:00:00";

    $Books = &returnBooks(@Group, @Env, @Timekey);

    foreach $Book(keys %Books){
    print "loop: ".$Book."\n";
    }

    sub returnBooks{
    # Extract parameters
    my ($x, $y, $z) = @_;


    .......

    Open file and create array

    if ($BusinessGroup eq $GROUP) {
    $BusinessGroups{$Book}=$BusinessGroup;
    }

    .......

    return \%BusinessGroups;
    matg, Jan 10, 2006
    #1
    1. Advertising

  2. matg

    Joe Smith Guest

    matg wrote:
    > I'm sure this is a basic Perl question but I'm a newbie and can't seem
    > to get my head around it.
    >
    > $Books = &returnBooks(@Group, @Env, @Timekey);


    The & is not needed; you're better off without it.

    > foreach $Book(keys %Books){
    > print "loop: ".$Book."\n";
    > }


    foreach my $Book (keys %$Books) { ... }

    -Joe
    Joe Smith, Jan 10, 2006
    #2
    1. Advertising

  3. matg

    matg Guest

    Perfect!
    Thanks so much for the speedy response.
    matg, Jan 10, 2006
    #3
  4. matg

    Anno Siegel Guest

    matg <> wrote in comp.lang.perl.misc:
    > I'm sure this is a basic Perl question but I'm a newbie and can't seem
    > to get my head around it.
    >
    > I want to pass 3 parameters eg. ("WM","windows","30/12/2005 00:00:00")
    > to a subroutine that will open a file and return an associative array
    > to the calling procedure - I believe the issue may lie in the way I
    > call the sub - any clues? The sub executes correctly and does create
    > the array but I can't seem to read anything from it once it is returned
    > (i.e. the foreach loop produces nothing)
    >
    > Abridged code follows


    No strict? No warnings?

    > @Group = "WM";
    > @Env = "windows";
    > @Timekey = "30/12/2005 00:00:00";
    >
    > $Books = &returnBooks(@Group, @Env, @Timekey);
    >
    > foreach $Book(keys %Books){


    The hash %Books has nothing to with the scalar $Books. You must
    de-reference the scalar:

    foreach my $Book ( keys %$Books ) {
    # ...
    }

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Jan 10, 2006
    #4
  5. matg

    Brad Baxter Guest

    matg wrote:
    > I want to pass 3 parameters eg. ("WM","windows","30/12/2005 00:00:00")
    > to a subroutine that will open a file and return an associative array
    > to the calling procedure - I believe the issue may lie in the way I
    > call the sub - any clues? The sub executes correctly and does create
    > the array but I can't seem to read anything from it once it is returned
    > (i.e. the foreach loop produces nothing)
    >
    > Abridged code follows


    "Abridged" is good. "Working" would be better. :)


    > @Group = "WM";
    > @Env = "windows";
    > @Timekey = "30/12/2005 00:00:00";
    >
    > $Books = &returnBooks(@Group, @Env, @Timekey);


    Even though this happens to do what you intended in this case,
    this is very much not a good thing to do. It is better to use
    scalars when scalars is what you're dealing with, e.g.,

    my $Group = "WM";
    my $Env = "windows";
    my $Timekey = "30/12/2005 00:00:00";

    my $Books = returnBooks($Group, $Env, $Timekey);

    --
    Brad
    Brad Baxter, Jan 11, 2006
    #5
  6. matg

    matg Guest

    Thanks for all the input. Can anyone explain to me why I can't read
    diectly from the return as follows:

    Instead of :

    foreach my $Book(keys %$Books) {
    if ($Book eq ($PositionSourceSystem."_".$RiskClass)) {
    $GoodBook = 1;
    }
    }


    Why can I not do instead:

    if (exists($Books{($PositionSourceSystem."_".$RiskClass)})) {
    $GoodBook = 1
    }

    Clearly this does not work but is there an alternative to the first
    example of iterating over every book in the array?
    matg, Jan 13, 2006
    #6
  7. matg

    Paul Lalli Guest

    matg wrote:
    > Thanks for all the input.


    Who are you thanking? For what input? Please quote some context when
    posting a reply to Usenet. Keep in mind this is not a forum, a
    bulletin board, nor a Google Group.

    (Have you read the Posting Guidelines for this newsgroup yet?)

    > Can anyone explain to me why I can't read
    > diectly from the return as follows:
    >
    > Instead of :
    >
    > foreach my $Book(keys %$Books) {
    > if ($Book eq ($PositionSourceSystem."_".$RiskClass)) {
    > $GoodBook = 1;
    > }
    > }
    >
    >
    > Why can I not do instead:
    >
    > if (exists($Books{($PositionSourceSystem."_".$RiskClass)})) {
    > $GoodBook = 1
    > }
    >


    Because there's no such variable as %Books. (You are using strict, so
    you were told that already by the compiler, right?)

    There is, however a variable $Books, which contains a hash reference.
    So there's no reason at all that you can't do:

    if (exists $Books->{$PositionSourceSystem . '_' . $RiskClass}) {
    $GoodBook = 1;
    }

    Paul Lalli
    Paul Lalli, Jan 13, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    6
    Views:
    410
    Mark A. Odell
    Dec 2, 2003
  2. Neo
    Replies:
    2
    Views:
    328
    Arthur J. O'Dwyer
    Dec 9, 2003
  3. Ben
    Replies:
    2
    Views:
    884
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,021
    Lawrence D'Oliveiro
    May 20, 2011
  5. rp
    Replies:
    1
    Views:
    518
    red floyd
    Nov 10, 2011
Loading...

Share This Page