in reply to removing doubles by comparing two strings - newbie question!
Hi, Anonymous:
Well, you have a couple of issues going on here, that are likely to confuse you. First, when you use a '$' for your variable's sigil (the sign in front), you're telling Perl that the variable is a scalar. It contains a single item of data, even though that item is "1, 2, 3, 4, 5, 6, 70". That is not a list, nor is it an array. It's a string. In order to create an array, you would use the '@' symbol, as in '@a'. This tells Perl that this variable will contain a set of scalar values, and that their order is important.
To set up an array, you can't do the quoted list of numbers. That would just create an array with a single element, containing the string. You were on the right track - separating the individual elements with a comma - but you need to place the list in parentheses, not quotes. The assignment would look like this: @a = (1, 2, 3, 4, 5, 6, 70); Once you've created your arrays, you can then try some of the things [id://ptum], [id://Tanktalus], or [id://GrandFather] have shown you. I am partial to using hashes myself, but that may not be something you've learned yet.
I might as well warn you now that it's generally a good idea to place two lines at the beginning of your code:
Those two 'pragmas' will save you much grief. They tell Perl to apply stricter rules to your code, meaning your code will show more things as errors and warnings. Although it's a real pain to get a bunch of warnings, it's good discipline to learn how to write code that avoids all the errors and warnings these pragmas cause. They're actually warning you about programming techniques that, although permitted, are very often ones that lead to problems. First, learn to write within the limitations of these pragmas. When you know what the rules are, then you'll know when it's a good idea to break them. At least, theoretically ;-)use warnings; use strict;
And finally, if you're not already confused enough: avoid using $a and $b as variables. They have a special meaning in Perl that could create difficulties for you if you some day need to use them (they're used for sorting). You'll be OK as long as you're not sorting anything, but it's just a good habit to avoid things that might some day lead to problems if you forget.
|
|---|