Results 1 to 10 of 13
What am I doing wrong in the following perl script :S
Code:
#!/bin/perl
$WORDS = "Firefox in Que";
open (FILE_READ, "~/.src/.starting");
@raw_data=<FILE_READ>;
close(FILE_READ);
$lines = 0;
foreach(@raw_data)
{
$lines++;
if ...
- 10-25-2007 #1
Problems with Perls
What am I doing wrong in the following perl script :S
The red lines are the Errors.Code:#!/bin/perl $WORDS = "Firefox in Que"; open (FILE_READ, "~/.src/.starting"); @raw_data=<FILE_READ>; close(FILE_READ); $lines = 0; foreach(@raw_data) { $lines++; if ($raw_data -eq $WORDS); { open(FILE_WRITE, ">~/.src/.starting"); foreach(@raw_data) { if($currentline -ne $lines) { print FILE_WRITE $raw_data; } } close FILE_WRITE; } else { open(FILE_WRITE, ">>~/.src/.starting"); print FILE_WRITE $WORDS; close FILE_WRITE; } }
I have been breaking my head for quite a while on this one.
- 10-25-2007 #2
On line 11, change the -eq to eq and remove the semicolon.
On line 16, change the -ne to ne.
While we're at it, might you want to change line 2 to this?
If not, you're going to have to change your code in three other places.Code:$WORDS = "Firefox in Que\n"
And if you really want to polish your code, you'll want to say "Queue". (grin)
Hope this helps.
- 10-25-2007 #3
- 10-25-2007 #4
Use a dash in front of eq when you're writing a bash script and you're comparing numerical quantities.
Use the eq without a dash when you're writing a Perl script and you're comparing strings.
A queue is a data structure. It's rather like a stack. But with a stack, the most recent item into it is the first item removed from it. With a queue (in its simplest form), the least recent item into it is the first item removed from it.
In the United States, when several people are gathered together to be served one at a time at a sales counter in the same order that they joined the gathering, that gathering is known as a line. In most other parts of the English-speaking world, that gathering is known as a queue. It works roughly the same as the data structure.
The English word "queue" comes from the Middle French (in which it had the same meaning, but presumably without the data processing application). It came to them from the Latin word "cauda", which means tail.
You can get more information about the data structure here.
The theory of how queues behave in real life has been addressed here.
- 10-25-2007 #5
- 10-25-2007 #6Not particularly stupid; it's a common error to confuse idioms when programming in two or more languages.I am just stupid by putting the dash there then
Another common one is for C programmers, remembering this construct in bash
to say this in C:Code:if [ $ABC = abc]
That doesn't compare a and b; it assigns a the value that's in b (and returns TRUE if the value is nonzero)!Code:if(a=b)
- 10-25-2007 #7
- 10-25-2007 #8
You're aware, I hope, that you can use the -Wall option in your compilation command line to find these problems?
- 10-25-2007 #9
- 10-25-2007 #10
Um, the -Wall option is for C and C++, not Perl. I seldom compile C or C++ without it.
Just so you know. (grin)


Reply With Quote