Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: Can't get the desired output..

by astronogun (Sexton)
on May 02, 2012 at 07:59 UTC ( #968382=note: print w/replies, xml ) Need Help??

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

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?


Replies are listed 'Best First'.
Re^3: Can't get the desired output..
by GrandFather (Sage) on May 02, 2012 at 08:38 UTC

    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:


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968382]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2018-07-15 23:37 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (328 votes). Check out past polls.