Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Can't get the desired output..

by davido (Archbishop)
on May 02, 2012 at 07:00 UTC ( #968374=note: print w/ replies, xml ) Need Help??


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:

  • 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


Comment on Re: Can't get the desired output..
Select or Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968374]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-12-25 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls