Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Nth position of array determind by <>

by RuZombieSlayer (Novice)
on Apr 23, 2016 at 09:30 UTC ( [id://1161296]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, i need help. I am trying to get the value of the nth position of a string. Where n = <>.

my £numbers = "2 1 3 4 5"; @numbers = split(' ', £numbers); //I am a beginer, i know this is inef +ficient but it helps me follow my work better. my $input = <>; // say iput is "3" // i want something similar to print $numbers[$input]; // prints 4 as 4 is the "3rd" position in numbers. //basically $numbers[3]; //but what is in [] can be decided by the input of <>

Replies are listed 'Best First'.
Re: Nth position of array determind by <>
by ww (Archbishop) on Apr 23, 2016 at 10:39 UTC

    You're using the wrong sigil, using an inapropriate comment marker, and providing nothing to prompt the user to enter a value. And that's just for starters.

    This looks like homework, by someone who hasn't bothered to study the text being used for the class. Please do so, and if it suggests something like this, then study Learning Perl.

    we expect SOPW to seek wisdom, not to ask us to do so for them.


    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.
      I'm sorry you feel so strongly, but i am not a student set homework... i'm at the end of my gcse years and learning perl as a "hobby" i am trying to teach myself and without much aid its quiet difficult and have learnt alot of the last week or so. Im sorry i am not very good at writing closed code but hey. The Standard input doesnt matter, surely you got the idea that there was a value in a string im trying to use that to call up a value in an array???! So as for you many accusations that were all wrong, could you actually give me some usefull advise instead of shoving your own head so far up your arse because you can code better than me... oh yeah, Thanks.

        Dear RuZombieSlayer,

        ww made four observations, and all of them were correct:

        1. You're using the wrong sigil,
        2. using an inapropriate comment marker,
        3. and providing nothing to prompt the user to enter a value.
        4. This looks like homework

        He did not say it was homework; he said it looked like homework. And it does.

        The fact that it is not homework is largely irrelevant.

        We are here to help, but only an open mind receives new information. An angry mind prone to taking everything personally is rarely open in any useful manner.

        Please consider the possibility that when you are asking for help, sometimes even your question will be part of the problem, and the only way you're going to get past it is to approach the problem with humility -- of the "maybe I could be wrong" variety.

        Responses like yours demonstrate the absence of the necessary mindset.

        If these words are not helpful for you, I will recede.

        Wishing you the very best in all things.

Re: Nth position of array determind by <>
by LanX (Saint) on Apr 23, 2016 at 11:03 UTC
    The shortest way is  (split(' ', $numbers))[$input]

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Nth position of array determind by <>
by Not_a_Number (Prior) on Apr 23, 2016 at 16:55 UTC

    It's nice to see the hegemony of US neo-imperialism being challenged by the use of the pound sterling sign (£scalar) instead of the dollar sign ($scalar).

    Personally, though, I'd prefer the euro sign (€scalar).

    ;-)

      To be really wildly creative, you might also use a hammer and a sickle for your sigils.

      By the way, in Perl 6, it would take just a couple of code lines to add the pound sign or the euro sign to the list of sigils understood by the Perl 6 grammar and to run a program understanding the £scalar or €scalar variable (see e.g. https://perlgeek.de/en/article/mutable-grammar-for-perl-6)

        I mediated once - approx 5000 posts ago - about introducing sigils like ¤£¥€ into perl 5 (for typed references, ie ¥hash etc ) but it wasn't really practical ... not only are they not part of 7 bit ascii (ie not included in many encodings) but also not available or hard to type on many keyboards around the world.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

      How does Larry feel about this? P5P? ;-).
      And if we're talking hegemony, how about some Asian currency symbols as sigils?

      Haha xD didn't see that, sorry. Wrote in kind of a rush and hit the key to the left of $ :/ ill change it now.
Re: Nth position of array determind by <>
by clueless newbie (Curate) on Apr 24, 2016 at 12:48 UTC
    Perhaps you're forgetting that perl's array start at 0? Thus the nth element of an array is actually n-1.
Re: Nth position of array determind by <>
by Marshall (Canon) on Apr 24, 2016 at 15:05 UTC
    Trying to learn programming on your own is a daunting task. I suggest getting a beginning book, perhaps "Perl for Dummies". I think you can get a used copy of this at a reasonable price. There are other books, but a lot of them start off too quickly for someone with no programming experience. I congratulate you on trying to learn a new skill.

    Here is some code for you to play with. There are many flaws in this, but I think you just wanted a simple demo of an input loop.

    #!/usr/bin/perl use warnings; use strict; my $numbers = "2 1 3 4 5"; my @numbers = split(' ', $numbers); print "numbers are: @numbers\n"; my $input; print "Enter numbers 1-5, q to quit:\n"; while ($input =<STDIN>, $input !~/q/i) { chomp $input; #needed to get rid of line ending print "the $input th num: $numbers[$input-1]\n"; } print "program completed\n"; __END__ numbers are: 2 1 3 4 5 Enter numbers 1-5, q to quit: 3 the 3 th num: 3 5 the 5 th num: 5 q program completed
    Update: saw this free beginning Perl online book. Price is right! Still goes very quickly for a non-programmer.

Log In?
Username:
Password:

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

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

    No recent polls found