in reply to Can't get the desired output..

The answers above are all important to your effort to learn Perl. Pay close attention to that advice. But if I understand your latest node correctly, there's another aspect you need to learn about: regular expressions.

This is not the spot for a full regex tutorial but it appears to me that you need to use a capture to do what you want:

if ($name =~ /(Cobray)/){ say "$1";}

The parentheses tell the regular expression engine to capture their content (if that content exists in the line (of 3, comma-separated names, in this case) to the special variable, $1.

Just a side note: I've written the code this way to stay close to your original... and to make the illustration fairly clear. But good practice calls for an intermediate step; assigning $1 to a named variable that won't be transient nor have the properties (esp. durability) of $1 that make it retain a prior match, if a new attempt fails.

Replies are listed 'Best First'.
Re^2: Can't get the desired output..
by locked_user sundialsvc4 (Abbot) on May 02, 2012 at 15:19 UTC

    Well said.   One fundamental principle in the Perl community is:   “TMTOWTDI™ = There’s More Than One Way To Do It.”   And there’s a lot more to that notion than first meets the eye.   Perl has many different well-tested tools for tearing strings apart and for representing the data that you’ve ripped out of a text file.   (And a great many other pursuits, as well.)   Every person who has responded and who will respond to the thread is approaching the problem in a different way (making certain guiding assumptions), and applying the various language features in different ways toward that end.   Notice, therefore, each direction that was taken, what premises were set forth, and what assumptions were made.   Consider why.   There are an abundance of features to choose from in this language; some of these, at first blush, may seem quirky and odd.   There is no “one way,” and that’s exactly what makes this pragmatic and very-practical language so powerful.   It has been called the Swiss Army® Knife of computer programming, and this statement is true.

    When you first encounter Perl, especially if you’ve used other languages, you tend to think, “Okay, what’s the way to do it, and I will learn that, and then:   I Have Learned Perl.™”   You very quickly realize that it’s just not that way.   The sheer diversity of how different people solve the same problem using the same tool can be downright confounding and off-putting at first.   Then you begin to see why the community calls itself, affectionately, Perl Monks.   They’re not being cagey or obtuse ...