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:
- code
- verbatim error and/or warning messages
- a coherent explanation of what "doesn't work actually means.
| [reply] |
|
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.
| [reply] |
|
Dear RuZombieSlayer,
ww made four observations, and all of them were correct:
- You're using the wrong sigil,
- using an inapropriate comment marker,
- and providing nothing to prompt the user to enter a value.
- 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.
| [reply] |
|
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).
;-)
| [reply] [d/l] [select] |
|
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)
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
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.
| [reply] |
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]
| [reply] [d/l] |
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. | [reply] |
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. | [reply] [d/l] |