in reply to Re^2: Can't get the desired output..
in thread Can't get the desired output..

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

Replies are listed 'Best First'.
Re^4: Can't get the desired output..
by astronogun (Sexton) on May 02, 2012 at 09:28 UTC

    Ok sorry for that.. Actually the input file was came from a .txt file. So the contents of the file is the list of names that was separated only by comma. So here's what looks like:

    Magnunm,Cobray,Ingram,Samsy,Rayver

    The code starts by opening the file and putting it in a array then I make it in to a string, then on line 5 putting it in a string again. then the if statement wherein it will suppose to be only get the name "Cobray" on that list:

    The requirement is to output only the chosen name like on this code it suppose to be output the name Cobray.. As you've said that it will output the whole thing.. How can I pull this off? hehe..

    Here's the code
    open my $infile, "<", "name_list.txt" or die "cannot open input: $!"; my @infile1= <$infile>; close $infile; foreach my $name (@infile1) { chomp $name; if ($name =~ /Cobray/){ print "$name";} }

    If I made my input like this:

    Magnunm Cobray Ingram Samsy Rayver

    it can display the name only... Hope that this info help you what I'm trying to achieve.. Thanks

      Recapping my understanding of your problem to see if I've it it right:

      If you put names into a file one per line then your program can be used to tell you that a line contains a specific name and you can print just that name by printing the line from the file. However if you use a file containing lists of names then you get the whole line printed and that is not what you want.

      However I suggest that isn't all your problem because you could just:

      ... if ($name =~ /Cobray/) { print "Cobray\n"; } ...

      So what is the bigger picture? Where is your real problem?

      True laziness is hard work
      If you want to print out "Cobray" if the string contains "Cobray", then why don't you just do that? Change your print "$name" to print "Cobray".

        yes, that was the lasting in my mind to do.. if I do this then "what's the point of learning line 7" for what I've read in a tutorial it will output that name (for sure) and as an add up to my knowledge it does print the whole thing as long as there is the name "Cobray" on it. Actually to tell you all I didn't know that it will turned out like this what I'm thinking is that if the code runs it will just output name "Cobray" So sorry for being such a noob.. That's why Im running this test for better understanding..

        What im after is how can I print that name without changing the input file.. For what I see the only solution is using either split or grep.. Is there other way if i dont want to use those.. thanks sir JavaFan,GrandFather and davido :)