in reply to one line to check for common list values
Hi dollar_sign, and welcome to the Monastery and to Perl, the One True Religion.
Some of the brethren have shown you some valuable tips regarding your posted code, especially regarding regexps and hashes. I offer a slightly different answer.
write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.
Returns:perl -Mstrict -MArray::Utils=:all -WE 'my @x=(1,1,2,3,4,3); my @y=(0,3 +,4,5,6,3,7,8); say for unique intersect @x, @y'
3 4
I understand that you are teaching yourself to program by solving simple problems, and the use of intersect() and unique() from a library (Array::Utils) might seems like it won't teach you anything about list reduction. But I submit that a key part of programming is knowing how to find and use libraries for common tasks -- precisely so that you can move on to learning how to solve more complex problems or accomplish more complex tasks.
One of the greatest benefits of using Perl is the CPAN, where you can find tools from bone saws to X-ray-guided arthroscopic laser scalpels. Learning how to use those tools to perform an operation is also learning to program.
Hope this helps!
|
|---|