http://www.perlmonks.org?node_id=968374


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

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:

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

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

    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

        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