Lunarpages Web Hosting Forum

Author Topic: Perl code problem. (New ro Perl)  (Read 3526 times)

Offline 47001foo

  • Trekkie
  • **
  • Posts: 10
    • http://atulshah.com
Perl code problem. (New ro Perl)
« on: July 08, 2003, 01:04:27 PM »
Gurus,
I am working on this perl code that reads a line from a text file. When I come across a particular line I need to do something. Hence i am checking for that line, but it goes into the if statement all the time and prints all the contents from my text file. My text file has a line that just has a word called "header". Why does it go into the "if" block always? I have also used  if (chomp($line) == 'header') and nothing. Do I  need to get the text file contents into an array like @fileline = <MAIN>; and then loop into the array? Cause my aim is to get the line that has the word "header" and skip to the next line. Also, need help with how to skip the file pointer to the next line.

# --- Perl Code ---
#!/usr/bin/perl
$main = "main.txt";
open (MAIN, "+<$main");
while ($line = <MAIN>) {
   if ($line == "header") { # Check line for 'header'
      print $line."<BR>";
   }
}
close (MAIN);
# --- End Perl Code ---
--- main.txt file content ----
header
---------------------
Name: Shoo shii
Subject: test
Details: Some Details
---------------------
--- End main.txt file content  ----

Thanks,
47001foo

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Perl code problem. (New ro Perl)
« Reply #1 on: July 08, 2003, 09:15:57 PM »
Just a thought.. I"m going off the top of my head here.. its been a while since I touched perl! But.. is == the correct comparison method to use for a string comparison? Or is there another command you need to be using. Because the == "" might be always returning true by default because its the wrong comparison operator!

- Just  a thought

- Ed

Offline 47001foo

  • Trekkie
  • **
  • Posts: 10
    • http://atulshah.com
Ed!
« Reply #2 on: July 09, 2003, 07:39:18 AM »
Even though I am new to perl I think the string comparison in any language is always '==' cause only '=' will be the assignment operator.

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Perl code problem. (New ro Perl)
« Reply #3 on: July 09, 2003, 07:42:15 AM »
in C its strcmp(string1, string2)

let me go look it up....

ok ...
== is a numeric comparison.
eq is the string comparison.

Try replacing "==" with "eq"

- Ed

Offline 47001foo

  • Trekkie
  • **
  • Posts: 10
    • http://atulshah.com
Perl code problem. (New ro Perl)
« Reply #4 on: July 09, 2003, 07:52:22 AM »
Cool! I completely forgot about the string comparison. Another thing is how do i move the file pointer to the next line?

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Perl code problem. (New ro Perl)
« Reply #5 on: July 09, 2003, 09:26:43 AM »
A cheap way is to just call it a second time, but dont do anything with it. That should advance it.
Since its in a while loop, just go once through the loop doign nothing.

What exactly are you trying to do?.. make it keep moving down until it finds "header" then print out everything after that?

- Ed :-)

Offline 47001foo

  • Trekkie
  • **
  • Posts: 10
    • http://atulshah.com
Skipping file pointer
« Reply #6 on: July 09, 2003, 01:56:16 PM »
Yes, I called it the second time e.g. $line = <MAIN>; and it did skip that to the next line.
BTW, I am trying to create a guestbook of my own as a learning tool. I just used the term 'header' for my practise(this post). In my guestbook I get the form data and post it into a empty dummy file. From the main guestbook file I get the other posts and append that to that dummy file, below the new post. Rename that main guestbook file to a tmpfile. Rename the dummy file to the main guestbook file and then rename the tmpfile to that dummy file with no data. This is just because I want the newest post on the top everytime. I could not find a way(there is no way I think) to insert text in between lines. So I am using to get data between <!-- start posts ---> and <!-- end posts --> from the main guestbook and dump it after the new posts and again stamp it with <!-- start posts --> and <!-- end posts -->. And above and below the posts I do not want all the html, JS and CSS everytime.
I hope this is the right method of doing stuff. It is learning only. I will probably post the working code and the actual guestbook once I am done.

Hope you get this explanation.

47001foo

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Perl code problem. (New ro Perl)
« Reply #7 on: July 09, 2003, 06:16:36 PM »
Have you thought of reading the entire file into an array, or memory of some sort. Then printing out starting with the newest entry, and then dumping the array after it.

Just an idea..

- Ed

Offline 47001foo

  • Trekkie
  • **
  • Posts: 10
    • http://atulshah.com
Perl code problem. (New ro Perl)
« Reply #8 on: July 10, 2003, 06:20:30 AM »
Well, as per my first post I thought of doing @fileline = <MAIN>; if I was not able to fix my "if" statement. Also, don't you think that putting it in an array would have some kind of impact on the processing if the file was very large.

Good thought though!

47001foo

Offline Ed

  • Berserker Poster
  • *****
  • Posts: 5156
    • Joke A Whenever
Perl code problem. (New ro Perl)
« Reply #9 on: July 10, 2003, 06:55:37 PM »
Simply renameing the file is probably the best method. The memory is an issue, but not until you really get a huge guestbook file! There is an option to append to the file, but it woudl be at the end instead of the front.

- Ed

Offline ovenman

  • Spacescooter Operator
  • *****
  • Posts: 36
    • http://www.ovenman.com
Perl code problem. (New ro Perl)
« Reply #10 on: July 14, 2003, 09:32:15 AM »
First off, pic up a book on Perl.  I suggest "Learning Perl" published by O'Reilly.  An excellent book, readable, sometimes funny (in a nerdy kind of way) and will let you learn the perl language in the least painful way possible.

Perl handles scalar variables ($var) differently from other languages.
Perl has two kinds of "equals" expressions.

eq for an alphanumeric comparison
== for numeric comparison

For character data:

$var1 = "A";
$var2 = "B";

if ($var1 eq $var2){
    #code executes if true
}

if ($var1 == $var2){
    #this will ALWAYS execute if both variables are character
    #because you use the == operator, perl converts the variables
    #into numbers, the way perl does this is by making both of them
    # the number 1 - therefore (1 equals 1) = true
}


For numeric Data:
$var3 = 123;
$var4 = 456;

if ($var3 == $var4){
   #code executes if true
}
Constantly expanding my online real estate.  Please visit me and my work.

Avocation: http://www.socialeats.com
Personal: http://www.ovenman.com
Vocation: http://www.beaulido.com
Client: http://www.footesamishshedsupstate.com