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

Hi There,

I am currently learning Perl as part of my NVQ and I am getting very confused as the practice exam questions have been written wrong and I am unsure on what is the correct answer. here is are two examples:

http://temerity.co.uk/dev1/perlerrors.png
http://temerity.co.uk/dev1/perlerrors2.png

Please can someone advise me on what is right and what is wrong

Many Thanks

Jack

Replies are listed 'Best First'.
Re: Please Help With Perl
by marto (Cardinal) on May 21, 2013 at 11:21 UTC

    I think it's also worth pointing out a few things since you're new here. PerlMonks for the Absolute Beginner is a nice short tutorial giving an overview of the site. It's perhaps easier to post the actual questions and data rather than a screenshot that people may or may not look at. Check out How do I post a question effectively?. Finally advice on selecting a meaningful title How do I compose an effective node title?. This may seem overwhelming at the moment, given that you've simply asked one question. I do advise taking the time to work through it in order to get the most out of the site.

    Update: fixed typo.

Re: Please Help With Perl
by marto (Cardinal) on May 21, 2013 at 10:33 UTC

    Hi, for clarity can you confirm that the three sections of the image you have posted are the question, the answer and an explanation?

    If we take the first image you've posted it then it's correct to say that there will be no output to speak of. Clarity can be gained by adding use warnings; to the example code:

    #!/usr/bin/perl use warnings; @this_array = ("b", "a", "d", "c"); foreach($this_array){ print("$_\n"); }

    Which will return:

    Use of uninitialized value $_ in concatenation (.) or string at derp.p +l line 7.

    By simply changing the code to:

    #!/usr/bin/perl use warnings; @this_array = ("b", "a", "d", "c"); foreach(@this_array){ # note @this_array not $this_array print("$_\n"); }

    We now have working code which prints each item within the array. This is discussed in the third pannel (the explanation?). Out of curiosity is this a distance learning thing?

Re: Please Help With Perl
by tobyink (Canon) on May 21, 2013 at 10:30 UTC

    Line 2 for the first question should be:

    foreach(@this_array) {

    ... because @this_array and $this_array are different and unrelated variables.

    And for the other question, line 2 should be:

    %car_hash = reverse(%car_hash);

    ... again because $car_hash and %car_hash are different variables, but also because reverse() returns the reversed hash, so you need to assign it somewhere.

    I'm surprised the exam questions contain such basic errors.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      "I'm surprised the exam questions contain such basic errors."

      Perhaps this is the purpose of the question, to introduce a problem and ask what they output will be, and why.

Re: Please Help With Perl
by Random_Walk (Prior) on May 21, 2013 at 10:35 UTC

    Have a read through http://perldoc.perl.org/perldata.html The two examples given both define one type of data store (array, hash) and then refer to another type of data store (scalar). Once you have read the above link play with some code and post code you can not get working.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Please Help With Perl
by choroba (Cardinal) on May 21, 2013 at 10:25 UTC
    If I understand the pictures correctly, the code on the left is wrong, while the second and third columns are right.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Please Help With Perl
by ww (Archbishop) on May 21, 2013 at 10:38 UTC
    I don't happen to know what an "NVQ" is -- nor care -- but I'll take $ to donuts that it's related to production of income.

    So here's today's deal-of-all-deals: send \me (upfront) USD 100K, a small fraction of your likely lifetime income and 10% of each subsequent year's gross (with your firstborn as security)

    ... and I'll do this piece of

    your

    work for you.
    On the other hand, if this is other than an extremely amateur-ish phishing expedition, perhaps you'll take the trouble to actually post the msterial, and a problem statement more precise than the tenditious statement that the "practice exam questions have been written wrong", I may be inclined to discount my fees.

    In common with some other Monks, I'm extremely averse to visting unknown sites to acquire unknown malware.

    UPDATE: Based on my British cousin's corrective explanations (but you should know you're addressing many nationalities, and, in fact, many native languages when you post here), I apologise for the sarcastic tone of my corrective note above (shakes head in chagrin: which took so long to draft without rechcking replies before posting that I replied-in-fact after the preceding nodes -- which cast much needed light -- were posted.

    Sorry.

    However, also in keeping with a note from marto, I really should have included this earlier:

    As to the dollars and donuts, I would argue (despite the downvoting which was majority when this was written) that in this case I should probably get both.   :-)


    If you didn't program your executable by toggling in binary, it wasn't really programming!

      'I don't happen to know what an "NVQ" is -- nor care -- but I'll take $ to donuts that it's related to production of income.'

      'On the other hand, if this is other than an extremely amateur-ish phishing expedition, perhaps you'll take the trouble to actually post the msterial, and a problem statement more precise than the tenditious statement that the "practice exam questions have been written wrong", I may be inclined to discount my fees.'

      This is quite the overreaction. A National_Vocational_Qualification is about work based training (undertaken while employed in some capacity) and assessment of said training, generally by people who have completed high school but have no further formal education. It doesn't necessarily relate to getting more money. People should always be free to ask questions of this nature without ridicule. They are relatively young, a new member and first time poster. Hopefully this won't give them too much of a bad impression.

      Update: s/over reaction/overreaction/

      An NVQ is a recognised "in-work" qualification in the UK. As a rough guide, they are qualifications earned whilst you are an apprentice in a job.

      I think you were a little quick on the trigger there, pardner! :-)

      P.S. Do we get the dollars or the donuts? :-)

      If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

      Dollars to doughnuts, this volume of the encyclopaedia covers a very small portion of the alphabet.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name