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

Hi I'm currently testing my code that will soon implemented on the main thing.. at my test I have this array @name and there there is an input "Magnum,Cobray,Ingram"

I want to output only "Cobray" but it displayed no output in cmd

Here's the code:

@name = ("Magnunm,Cobray,Ingram"); $name = "@name"; if ($name =~ /Cobray/){ print "$name";}

Is something missing? Thank you very much for all the help

Replies are listed 'Best First'.
Re: Can't get the desired output..
by davido (Cardinal) on May 02, 2012 at 07:00 UTC

    The snippet you posted, when run, displays Magnunm,Cobray,Ingram. Perhaps the code you posted isn't exactly the same as the code you're running.

    The code you did post has several problems. I'm a little reluctant to address them since it seems like you've posted code that is different from the code you're running. Nevertheless, here are some tips:

    • The first line assigns a single string, "Magnunm,Cobray,Ingram" to an array. It will be stored in $name[0].
    • The second line interpolates @name into a string. There's only one element. The resulting scalar value is a string, which you assign to $name.
    • Next you test to see if $name contains text that matches Cobray. It does, of course. So you end up printing the entire string.

    You'll have to tell us more about what you're starting with, and and what code you're actually running so we can nudge you in the right direction.


    Dave

      Yes I retested it and displayed the whole value of the string sorry for that, actually im studying the part on the if statement. What Im doing is just print the ones what I wanted to see. Because Im still learning.

      Regarding on the 3rd tip is there a way that I could get on ly the "Cobray" name instead of getting the whole line because it has the word Cobray on it. Or it has something to do with the commas that's why it displayed the whole thing of $name?

      Thanks

        The short answer is: 'Yes.'

        A same length answer is: 'Why?'

        A clarification of both answers is: If you have a hard wired match to 'Cobray' then you already know exactly what was matched so what further are your trying to achieve? If you tell us what your larger goal is we probably can help much more.

        I suspect you haven't understood davido's reply, especially the first two points. If that is the case please let us know because the key to what you want to do most likely lies in understanding those aspects of Perl and programming.

        True laziness is hard work
Re: Can't get the desired output..
by ww (Archbishop) on May 02, 2012 at 11:54 UTC
    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.

      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 ...

Re: Can't get the desired output..
by locked_user sundialsvc4 (Abbot) on May 02, 2012 at 11:41 UTC

    My first instinct would be to use split() on a comma.   And then maybe use a negative array-index to get to the last element, e.g.:

    $ perl -e 'my $foo="Moe,Curly,Irene"; my @bar=split(/,/, $foo); print @bar[-1]."\n";' Irene