wcj75019 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: I just want IF, OR, OR not every one
by chromatic (Archbishop) on Feb 29, 2008 at 00:32 UTC

    Without seeing some sample input data and your desired output, I'm not sure anyone can help you here. Your question is basically incoherent without that.

    Can someone explain to me how if works?

    It's pretty simple. Does this code make sense?

    sub print_if_true { my $value = shift; if ($value) { print "Value '$value' is true!\n"; } } print_if_true( 1 ); print_if_true( 0 );

    Now if you throw an else block in there, things should still make sense:

    sub print_if_true { my $value = shift; if ($value) { print "Value '$value' is true!\n"; } else { print "Value '$value' is false!\n"; } }

    I hope that makes sense. There's one thing that you should also know.

    sub print_if_true { my $value = shift; if ($value) { print "Value '$value' is true!\n"; } else { print "Value '$value' is false!\n"; } print "This always prints.\n"; }

    If any of this is surprising, then please let us know so that we can explain it. I think things will make sense for you in your program when you understand this.

Re: I just want IF, OR, OR not every one
by merlyn (Sage) on Feb 28, 2008 at 22:07 UTC
      Yes, I did read from the last time I posted. It did not help. That is why I reworded it. It is NOT if/else. It is if elsif elsif I asked the question sarcastically if someone could explain it. Becau +se I can't get it to work right.
Re: I just want IF, OR, OR not every one
by swampyankee (Parson) on Feb 28, 2008 at 23:23 UTC

    This statement:  if($line =~ /!bgs/){  is syntactically correct, but probably isn't doing what you want: it's checking to see whether $line contains the string "!bgs" somewhere, not the rather longer string defined by your here document. Presuming $bgs is just a string, and not a command to be executed you may want something more like this:

    while(<SH>){ if(/$bgs/) {# Note $ means it's a variable, to be interpolated; ! +doesn't #do processing for true case } else{ #do processing for false case }

    You really, really should study the documentation. I think Perl has quite clear, well organized documentation (it's not perfect, but then producing perfect documentation is at least as hard as producing perfect code); you're missing something. I also think you may be failing to tell us something. Your $bgs string looks a lot like a command, and if you want to capture its output, you'll have to use backticks.


    emc

    Information about American English usage here and here.

    Floating point issues? Read this before posting: http://docs.sun.com/source/806-3568/ncg_goldberg.html

      I think you are close, but still you don't get a cookie!
      I'm trying to test to see if bgs is in the file.

      if it is not. I add the here document $bgs above syslogd.

      But, if it is there. It adds the $here document anyway.

      Perhaps the OP wanted to test if $line does NOT contain the literal string 'bgs' and was what he really meant to write $line !~ /bgs/ rather than $line =~ /!bgs/

      Then of course his first test falls through (although he thinks erroneously that it succeeds) and to his utter confusion the other conditions in the elsif chain get tested until one succeeds. Hence his question about how the if -- elsif -- else construct works and how to avoid multiple results in this chain to apply, which got us all confused as only one condition can succesfully apply.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Count, That that was a good thought. I like where your head is. But, that continuously prints $bgs here documents. Thanks,
Re: I just want IF, OR, OR not every one
by ysth (Canon) on Feb 29, 2008 at 01:58 UTC
    Try reading the whole file into a string (one way is use File::Slurp "read_file"; $all_lines = read_file("filename");) and working on that string instead.
      That is one way. They are big files. There are 650 of them. I don't want to crash the box.
Re: I just want IF, OR, OR not every one
by CountZero (Bishop) on Feb 29, 2008 at 06:56 UTC
    Hi Bill,

    Next time, please don't put all of your node inside <code> ... </code> tags please. It is not nice to look at.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James