newbie baffled by de/referencing with subroutines

Discussion in 'Perl Misc' started by Jessica, Apr 10, 2005.

  1. Jessica

    Jessica Guest

    I created an elaborate array (my @uniqueParsed) inside a subroutine
    and I want to use data from that array after the sub is finished. I
    declared a reference inside the sub:

    my $uniqueParsed_r = \@uniqueParsed;

    then tried to print the data outside the sub by this dereference:

    print "dereferenced array is @$uniqueParsed_r \n";

    but nothing was returned (i.e., @$uniqueParsed_r is empty). By the
    way, if I put the print statement inside the sub, the dereferenced
    array prints correctly.

    What's wrong with my reference and/or dereference, and how do I fix it?


    Thanks!
     
    Jessica, Apr 10, 2005
    #1
    1. Advertising

  2. "Jessica" <> wrote in
    news::

    > I created an elaborate array (my @uniqueParsed) inside a subroutine
    > and I want to use data from that array after the sub is finished. I
    > declared a reference inside the sub:
    >
    > my $uniqueParsed_r = \@uniqueParsed;
    >
    > then tried to print the data outside the sub by this dereference:
    >
    > print "dereferenced array is @$uniqueParsed_r \n";
    >
    > but nothing was returned (i.e., @$uniqueParsed_r is empty). By the
    > way, if I put the print statement inside the sub, the dereferenced
    > array prints correctly.
    >
    > What's wrong with my reference and/or dereference, and how do I fix
    > it?


    Have you seen the posting guidelines posted here regularly?

    The guidelines contain valuable information on how to help others help
    you, do read them.

    I have a feeling you are missing:

    use warnings;
    use strict;

    in your code.

    I will venture to guess that you are not returning the reference from
    your sub. In that case, $uniqueParsed_r only exists within the lexical
    scope of the subroutine.

    Please post a short example script that exhibits the problem so that we
    don't have to guess what you are actually doing.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 10, 2005
    #2
    1. Advertising

  3. Jessica

    Jessica Guest

    Thanks for your reply, Sinan. You're right, I haven't checked the
    posting guidelines (abject apologies from this thoroughly befuddled
    newbie). Also I did not use warnings or strict, as that's not what our
    instructor has us do, for better or worse.

    As far as sample code, here's a sketch. The actual code that generates
    @uniqueParsed is long and probably irrelevant, so I'll kinda skip over
    it:

    sub parseGff
    {
    do lots of stuff ...
    my @uniqueParsed = ();
    put stuff into @uniqueParsed ...
    my $uniqueParsed_r = \@uniqueParsed;
    some more stuff that doesn't matter here ...
    }

    parseGff

    print "dereferenced array is @{$uniqueParsed_r} \n";



    So you're right, I don't return anything - and that's the part that has
    me baffled. How do I return a reference from the sub? (I have zero
    background in programming, it's only the 5th lecture and we're doing
    references - the instructor is going too fast for newbies like me.)

    Thanks again for your help!
     
    Jessica, Apr 10, 2005
    #3
  4. "Jessica" <> wrote in
    news::

    > Thanks for your reply, Sinan. You're right, I haven't checked the
    > posting guidelines (abject apologies from this thoroughly befuddled
    > newbie). Also I did not use warnings or strict, as that's not what
    > our instructor has us do, for better or worse.


    For worse. Do read the guidelines for the justification for using them.
    Then, demand that your instructor use them. If not, either ask him/her
    to explain why. If the explanation does not satisfy you, demand your
    money back. :) I know this sounds a little over the top, but I can see
    no reason why warnings and strict should be turned off when learning.

    > As far as sample code, here's a sketch. The actual code that
    > generates @uniqueParsed is long and probably irrelevant, so I'll kinda
    > skip over it:
    >
    > sub parseGff
    > {
    > do lots of stuff ...
    > my @uniqueParsed = ();
    > put stuff into @uniqueParsed ...
    > my $uniqueParsed_r = \@uniqueParsed;
    > some more stuff that doesn't matter here ...
    > }
    >
    > parseGff
    >
    > print "dereferenced array is @{$uniqueParsed_r} \n";
    >
    >
    > So you're right, I don't return anything - and that's the part that
    > has me baffled. How do I return a reference from the sub?


    OK, I get the picture now.

    Here is a short example to get you started:

    #! /usr/bin/perl

    my $array_ref = select_letters();
    print "@$array_ref\n";

    sub select_letters {
    my @letters;

    for my $letter ( 'A' .. 'Z' ) {
    if(rand(1) < 0.5) {
    push @letters, $letter;
    }
    }

    return \@letters;
    }

    __END__

    > (I have zero background in programming, it's only the 5th lecture
    > and we're doing references - the instructor is going too fast for
    > newbies like me.)


    Perl comes with extensive documentation.

    Depending on your platform, you can access this documentation in a
    variety of ways.

    On Windows, with ActiveState Perl, you can find documentation in HTML
    format via the Start menu.

    On all platforms, you can use the command line program perldoc to view
    documentation. You might want to consult:

    perldoc perlreftut

    and

    perldoc perlsub

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 10, 2005
    #4
  5. Jessica <> wrote:
    > I created an elaborate array (my @uniqueParsed) inside a subroutine
    > and I want to use data from that array after the sub is finished. I
    > declared a reference inside the sub:
    >
    > my $uniqueParsed_r = \@uniqueParsed;
    >
    > then tried to print the data outside the sub by this dereference:
    >
    > print "dereferenced array is @$uniqueParsed_r \n";
    >
    > but nothing was returned (i.e., @$uniqueParsed_r is empty). By the
    > way, if I put the print statement inside the sub, the dereferenced
    > array prints correctly.



    You should always enable strict and warnings when developing Perl code!


    > What's wrong with my reference and/or dereference,



    There is nothing wrong with either your reference nor your dereference.

    There is something wrong with your variable scoping.

    (I am forced to guess that that is what it is, because you did
    not include a short and complete program that I can run. Have
    you seen the Posting Guidelines that are posted here frequently?
    )


    > and how do I fix it?



    Don't declare a lexical variable that will cease to exist as
    soon as the subroutine returns, instead return a reference
    to the elaborate array.

    sub nameless_sub {
    my @uniqueParsed;
    # populate the array...
    return \@uniqueParsed;
    }

    my $uP = nameless_sub();
    print "dereferenced array is @$uP\n";


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 10, 2005
    #5
  6. Jessica <> wrote:

    > Also I did not use warnings or strict, as that's not what our
    > instructor has us do, for better or worse.



    You have a poor instructor.

    strict and warnings help find bugs in your code, take all
    of the help that you can get.


    > How do I return a reference from the sub?



    return \@uniqueParsed;


    > it's only the 5th lecture and we're doing
    > references -



    You have a poorly designed curriculum.


    > the instructor is going too fast for newbies like me.)



    You have a poor instructor.

    Sounds all-around poor. How much was the tuition?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 11, 2005
    #6
  7. Jessica

    Eric Bohlman Guest

    "Jessica" <> wrote in news:1113170027.017229.15810
    @g14g2000cwa.googlegroups.com:

    > Thanks for your reply, Sinan. You're right, I haven't checked the
    > posting guidelines (abject apologies from this thoroughly befuddled
    > newbie). Also I did not use warnings or strict, as that's not what our
    > instructor has us do, for better or worse.


    Can we have some clarification here? Are you saying

    1) Your instructor specifically told his class *not* to use warnings and
    strict? or

    2) Your instructor simply didn't tell his class to use them?

    If 1) is the case, then your instructor is B A D, period. He's
    deliberately teaching his students bad habits. In that case it looks
    like *all* the fault lies with the instructor.

    If 2) is the case, then your instructor isn't experienced enough in Perl
    to be teaching it; that's probably not so much his fault as the
    institution's fault for trying to cut corners in order to save money.
    But if that's the case, the worst thing any of his students can do is
    take the attitude "I don't need to do anything the instructor didn't
    specifically tell me to do"; that's really the same as "I won't learn
    this if it's not going to be on the test."

    Assuming that your goal in taking a Perl class is more ambitious than
    just being able to say "I took a Perl class once"--in other words,
    assuming that your goal is to eventually be able to write Perl programs--
    then you're hoping to reach a point where you *won't* have an instructor
    telling you what to do and there *won't* be any examinations. But once
    you reach that point, you *won't* know everything there is to know about
    Perl programming (since your name isn't "Larry Wall") even though you'll
    still need to know more about it. And that means being able to pick up
    stuff on your own. That means becoming aware of what are considered
    "best practices" in the field and observing them unless/until you're able
    to articulate why your needs would be better served by something else.

    When learning a programming language, you really *do* have to do a lot of
    self-study beyond what's taught in the classroom and what's part of the
    assigned coursework. Because if a class is taught properly, the
    classroom time and the assignments will be devoted to only those aspects
    of the language that are difficult to pick up through self-study; there
    simply isn't time for what amounts to the instructor reading the textbook
    or the widely-available reference material to his class.

    With Perl, you're fortunate in that you don't have to go digging around
    to find good reference material; it's already there on your computer!
    The one thing a lot of people have to get used to is that Perl's
    documentation is quite "information-dense" whereas most software
    documentation is "information-sparse" (full of screen shots, lists of
    menu items, and similar stuff that would be immediately obvious to anyone
    who's ever used the software). There's a lot of meat on those bones!
    But you don't have to eat it all at once. Just develop a basic mental
    model of how the material is organized, so that when you have a question
    nagging at you, you have some idea of where to look for the answer.

    And probably the single most important important thing you can do is to
    get in the habit of *always* looking for the answers to such questions,
    even if they seem trivial or not of immediate importance (and if the
    answers don't leave you completely enlightened, feel quite free to come
    here and ask for clarification; questions of the form "I was wondering
    about...It says in perlop that...But I still quite don't get...And I
    wonder why it's done that way" are valuable not just to you, but to other
    readers of the group, because they get people thinking, learning new
    things, and re-examining old assumptions. You can learn an awful lot
    simply by reading the responses to well-asked questions (WAQs) here).
     
    Eric Bohlman, Apr 16, 2005
    #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. Lloyd Dupont

    baffled...

    Lloyd Dupont, Sep 1, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,236
    Lloyd Dupont
    Sep 2, 2004
  2. John English

    JNDI (LDAP): totally baffled!

    John English, Jun 9, 2005, in forum: Java
    Replies:
    4
    Views:
    778
    John English
    Jun 11, 2005
  3. Mark Thomas

    Baffled!

    Mark Thomas, Oct 24, 2003, in forum: C++
    Replies:
    4
    Views:
    479
    Jonathan Mcdougall
    Oct 25, 2003
  4. Paul
    Replies:
    6
    Views:
    517
    Beauregard T. Shagnasty
    Jun 16, 2006
  5. Steve The Geek

    Baffled newbie SNMP script

    Steve The Geek, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    113
    Steve The Geek
    Sep 24, 2003
Loading...

Share This Page