wcj75019 has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: if or die!
by Corion (Patriarch) on Mar 01, 2008 at 16:41 UTC | |
Maybe the answers in I just want IF, OR, OR not every one or if elsif elsif prints all three. can help you? At least show the courtesy to link to your older nodes if you're going to make a new toplevel node about the same problem. You could also spend some more time at explaining what you want your program to do. In separate, short but still coherent sentences. Maybe. Instead. Of using. Punctuation where. *** It does not make. Sense. Also, providing some sample input and output makes it easier for us to see where your problem is. Also, maybe even provide the output you want as well - this gives us even more to work with. As to your problem, I don't understand what you're trying to do. But the following line makes no sense:
What is this line supposed to do? Why is it in your program? Looking further, the whole block makes no sense to me:
What is it supposed to do? How does it differ from the following?
| [reply] [d/l] [select] |
by wcj75019 (Acolyte) on Mar 01, 2008 at 17:33 UTC | |
If bgs isn't in the file. else go to syslogd. then after 3 lines. add the here document $bgs. Print to OUT file. I can't get it to work. If bgs isn't there. How do I add $bgs after the syslogd. | [reply] [d/l] |
by Corion (Patriarch) on Mar 01, 2008 at 18:01 UTC | |
I'm sorry, but I still don't understand you. English is not my first language, maybe it's not your first language either - please try to phrase your problem in longer sentences that do not start with "Else". I'm trying to rephrase your problem as I interpret it - please correct me if I get it wrong: Is that correct? To attack this problem, let's just reduce all we do to solving this small part and disregard all surrouding stuff. Once we've solved the small part, we can move towards solving the integration into your big program. For simplicity, let's assume we have the following input file:
And from how I interpret your task, this is what you want as output:
See how writing down what you have and what you want makes the problem much clearer? Now, on to your code: First, the line makes no sense at all. It just checks whether the current line contains "'syslogd')", but then ignores that knowledge completely. The line
Only assigns to $newline the number of the current line plus three. It does not advance the current file or anything. The lines
print out
immediately, instead of waiting some more lines. You could have told us that directly - by displaying what output you get. Anyway, take a look at the following program, which reads from the DATA filehandle and see if you understand how it does what it does:
| [reply] [d/l] [select] |
by wcj75019 (Acolyte) on Mar 02, 2008 at 01:53 UTC | |
by polettix (Vicar) on Mar 01, 2008 at 22:47 UTC | |
I would gladly link these together. I don't know how to do that.Need Help?? Linking on PerlMonks - What shortcuts can I use for linking to other information?, How do I link to nodes on this site by title?, How do I link to a node on this site by number?... Regarding the general style, Posting on PerlMonks can probably help. perl -ple'$_=reverse' <<<ti.xittelop@oivalf Io ho capito... ma tu che hai detto? | [reply] |
by wcj75019 (Acolyte) on Mar 02, 2008 at 17:58 UTC | |
by ww (Archbishop) on Mar 02, 2008 at 18:31 UTC | |
|
Re: if or die!
by chromatic (Archbishop) on Mar 01, 2008 at 19:56 UTC | |
Only problem: It fills every file with the stinkin here document. over and over. Yeah, it does. That's what your continue block does, if the conditional is true. That's what your else block does, if the conditional is not true. You need to stop commenting and uncommenting code and randomly adding and moving around code, because you're just making a mess and confusing yourself. Here is your first problem: } else { ($line =~ m/'syslogd'\)/); my $newline = $. + 3; $newline = $bgs; print OUT $newline; next; }</c>This block only executes if the current line does not contain bgs. That's probably most lines of the file. The regular expression does nothing. It's dead code. Delete it. The next line declares the variable $newline and assigns it a value. This is nearly dead code, because the subsequent line overwrites that value with the heredoc, which gets printed in the next line. Control flow then skips to the continue block, which prints the current line. You're not going to get this to work until you sit down, write out what you want to accomplish, list the necessary steps in order, and then translate that to code. Please do that; you'll save yourself a lot of time and trouble. | [reply] [d/l] [select] |
|
Re: if or die!
by shmem (Chancellor) on Mar 01, 2008 at 17:57 UTC | |
if or die! Well, it's die this time round, I guess. Your re-wording is bad, since it doesn't provide any further information. Here's my rephrasing, from what I can read from your code. Please correct that and post your correction. Hello monks, to which I would respond: See the documentation for open, or read perlopentut. If you open a file for output, you clobber it. A subsequent open for input just opens an empty file. You might want to use inplace edit, open it for update (+<) or write to a temporary file which you rename to the original after having closed the filehandle for writing. You could slurp in the file in one go and do a match like /('bgs\).*?;;)/s to have the complete bgs case branch in $1 - read perlretut and perlre if things are unclear here. You could also go through each file line by line, in which case you have to carry state variables you set and unset at the beginning and end of those case branches. You could weed out any eventual 'bgs' branches (by just ignoring them) and print your new 'bgs' branch right after the 'syslogd' branch, or before the closing 'esac' if it hasn't already been printed. update: One possible solution (if I understand rightly what you want to achieve):
--shmem
| [reply] [d/l] [select] |
by wcj75019 (Acolyte) on Mar 02, 2008 at 17:55 UTC | |
| [reply] [d/l] |
by shmem (Chancellor) on Mar 02, 2008 at 17:59 UTC | |
Unmatched ( in regex; marked by <-- HERE in m/^( <-- HERE \s*'syslogd'\).*?;;/ at b.t line 27 Yeah, closing paren missing. Oops :-)
--shmem
| [reply] [d/l] |
by ww (Archbishop) on Mar 02, 2008 at 18:06 UTC | |
Please, please: learn to proofread your code, rather than merely asking for that which you then cargo-cult. And please read the relevant documents or texts. | [reply] |
by ack (Deacon) on Mar 03, 2008 at 05:00 UTC | |
by Anonymous Monk on Mar 03, 2008 at 07:42 UTC | |
| |
by wcj75019 (Acolyte) on Mar 02, 2008 at 20:12 UTC | |
| [reply] |