Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Simple Perl If Else Statement

by funnylion (Initiate)
on May 15, 2014 at 21:10 UTC ( [id://1086214]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Simple Perl If Else Statement
by wjw (Priest) on May 15, 2014 at 21:32 UTC
    What have you tried? Show what you tried and someone will help. Ask someone to do your work and you will probably be asked for a going rate for code writing... :-) Gotta make some effort. You do and need help, you will almost certainly get help. But not if you don't show you tried...

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...
Re: Simple Perl If Else Statement
by GrandFather (Saint) on May 15, 2014 at 21:36 UTC

    Yes, you could do that. What's preventing you? What have you tried? What have "report columns" to do with anything?

    Maybe you could show us what you've tried and tell us why you think it's not working?

    Perl is the programming world's equivalent of English
Re: Simple Perl If Else Statement
by Lennotoecom (Pilgrim) on May 15, 2014 at 22:10 UTC
    dude, this?
    while (@a = split /\t|$/, <DATA>){ $a[0] =~ /\d/ ? print "K $a[0] : " : next; print "positive number\n" and next if $a[1] == $a[2]; print "negative number\n" and next if $a[1] == $a[3]; print "author didn't specify that option\n"; } __DATA__ K I B S 1 2 2 3 2 4 3 4 3 5 5 2 4 9 1 3
      Thank you Lennotoecom. I'm very new to Perl and still trying to get a handle on all of it. Instead of literally printing "positive number" or "negative number" how would you make the value reported from column K positive or negative depending on the values in I/B and I/S? Regardless this is a really good start and I can probably figure it out from here. Thanks!
        Ummmm, like this?
        while (@a = split /\t|$/, <DATA>){ next if $a[0] =~ /\D/; print "K: $a[0]\n" and next if $a[1] == $a[2]; print "K: -$a[0]\n" and next if $a[1] == $a[3]; print "author didn't specify that option\n"; } __DATA__ K I B S 1 2 2 3 2 4 3 4 3 5 5 2 4 9 1 3
Re: Simple Perl If Else Statement
by Laurent_R (Canon) on May 15, 2014 at 22:07 UTC
    Yeah, you've got quite clear and detailed specs, it is quite easy to turn it to code, I wish I had more often such easy specs. Please show what you have, I'll be happy to help if what you did does not work according to your specs.
      The spec is inconsistent in the special case where columns I, B, and S are all equal.
      Bill
        Really great point I didn't think of. I'll get back to the requester for clarification.
Re: Simple Perl If Else Statement
by GotToBTru (Prior) on May 16, 2014 at 20:44 UTC

    Data is possibly a spreadsheet.

    use strict; use warnings; my ($B,$I,$K,$S); while (<DATA>) { chomp; last unless ($_); ($B,$I,$K,$S) = (split /,/,$_)[1,8,10,18]; if ($I == $B) { $K = abs($K); } elsif ($I == $S) { $K = -1 * abs($K); } print "$K\n"; } __DATA__ 0,2,0,0,0,0,0,0,2,0,1,0,0,0,0,0,0,0,3 0,2,0,0,0,0,0,0,3,0,1,0,0,0,0,0,0,0,3 0,2,0,0,0,0,0,0,4,0,-5.5,0,0,0,0,0,0,0,3

    Output:

    1 -1 -5.5

    Update: $K = -1 instead of $K *= -1

    1 Peter 4:10

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1086214]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-23 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found