Closed Thread
Results 1 to 5 of 5

Thread: wrong variable?

  1. #1
    mtber is offline Newbie
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Unhappy wrong variable?

    i have a program that takes in the directory of a file and reads it in. It then stores that file in an array. the user is given the option of which regular expression they would like to search for in the document once the user makes a selection it runs that part of code in the corrosponding if statment. Then the code is suppose to print every line from the document that contains the expression being searched for.

    The problem is that the code does not print anything it is suppose to print each line of code that contains the expression being searched for but i get nothing when it is suppose to print i dont know if i have the wrong variable in the print in each of the if statements. can someone help!! please!

    Code:
    print"Please Enter the absolute file directory of the file you wish to read\n";
    $file = <STDIN>;
    open(file, $file);
    @file = <file>;
    
    print"\n1. Search for 'tion'\n";
    print"2. Search for one or more 'e'\n";
    print"3. Search for exactly 2 'e'\n";
    print"4. Search for a user Defined expression\n";
    print"5. Quit\n";
    print"Your Selection: ";
    $select = <STDIN>;
    chomp($select);
    
    if($select == 1)
    {
    	for($i=0; $i=scalar(@file); $i++;)
    	{
    		$result=@file=~/tion/;
    		print"$result";
    		#print"1\n";
    	}
    }
    if($select == 2)
    {
        $result=@file=~/e+/;
        print"$result";
        #print"2\n";
    }
    if($select == 3)
    {
        $result=@file=~/e{2}/;
        print"$result";
        #print"3\n";
    }
    if($select == 4)
    {
        print"\nPlease Enter a pattern: ";
        $pattern=<STDIN>;
        chomp($pattern);
        $result=@file=~/$pattern/;
        print"$result";
        #print"4\n";
    }
    if($select == 5)
    {
        #print"5\n"
        print"GoodBye";
        exit;
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: wrong variable?

    Your code threw an error for me on line 14:

    Code:
    	for($i=0; $i=scalar(@file); $i++;)
    Then I realized that you had a ; after the $i++. I removed that and it worked fine for me. I'm surprised that your compiler did not throw a syntax error.

  4. #3
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: wrong variable?

    He has already been told of this error on another forum, he posted this same question on 4 or 5 forums, probably school work.

    This is stilll wrong:

    Code:
    for($i=0; $i=scalar(@file); $i++)
    it should be:

    Code:
    for($i=0; $i < scalar(@file); $i++)
    otherwise it will probably just go into an infinite loop

  5. #4
    Jordan Guest

    Re: wrong variable?

    I didn't even catch the middle assignment in the for loop. Good eye.

  6. #5
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: wrong variable?

    well, I had an advantage, I saw the question several times.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-26-2010, 08:53 AM
  2. Anyone tell me what i'm doing wrong?
    By kentona in forum JavaScript and CSS
    Replies: 3
    Last Post: 09-03-2010, 02:51 AM
  3. What have I done wrong?
    By Turk4n in forum C and C++
    Replies: 27
    Last Post: 01-15-2009, 02:18 PM
  4. what's wrong with this?
    By martian in forum C and C++
    Replies: 6
    Last Post: 11-18-2008, 08:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

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