Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422

    Problems with Perls

    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 ($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;
     } 
    }
    The red lines are the Errors.
    I have been breaking my head for quite a while on this one.
    New Users, please read this..
    Google first, then ask..

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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?
    Code:
    $WORDS = "Firefox in Que\n"
    If not, you're going to have to change your code in three other places.

    And if you really want to polish your code, you'll want to say "Queue". (grin)

    Hope this helps.

  3. #3
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    Ah thanks.
    Can you explain me when I need to use a Dash infont of eq and when not?
    And what is a Queue? :S

    Thanks alot (Y)
    New Users, please read this..
    Google first, then ask..

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  5. #5
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    Ok thanks alot.

    I am just stupid by putting the dash there then

    Thanks for all the help ^^
    New Users, please read this..
    Google first, then ask..

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I am just stupid by putting the dash there then
    Not particularly stupid; it's a common error to confuse idioms when programming in two or more languages.

    Another common one is for C programmers, remembering this construct in bash
    Code:
    if [ $ABC = abc]
    to say this in C:
    Code:
    if(a=b)
    That doesn't compare a and b; it assigns a the value that's in b (and returns TRUE if the value is nonzero)!

  7. #7
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    Yea I always have the C problem since I a,m a C++ coder normaly
    New Users, please read this..
    Google first, then ask..

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You're aware, I hope, that you can use the -Wall option in your compilation command line to find these problems?

  9. #9
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    No I dont ^^
    I learned perl to myself without any documents.
    Just googling for the things i need everytime..
    Thanks
    New Users, please read this..
    Google first, then ask..

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Um, the -Wall option is for C and C++, not Perl. I seldom compile C or C++ without it.

    Just so you know. (grin)

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...