perl--logical help needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhumikas
    New Member
    • Aug 2007
    • 13

    perl--logical help needed

    I need a help with my code...
    What I want to do is:
    if there exists a directory_no, and found in the directories array , want to copy the contents of that array to a new array from where the directory_no is found to till the end of that array.
    In the end copy the new_directory array to the directories array;

    Ex:directories array: 1 2 3 4 5
    directory_no is 3.
    Output :new_directory 3 4 5
    in the end directories array: 3 4 5


    Code:
    if($directory_no)
      {
    foreach $directory (@directories)
    	  {
    	    $count++;
    	    print "$count";
    	    if($directory = $directory_no)
    	    {
                  for($count;$count<=@directories;$count++)
                  {              
                  $new_directory[i]=$directories[$count-1];
                  $i++;              
                  }              
    	      exit; 
    	    }
    	  }
    }
    @directories=@n ew_directory;
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Originally posted by bhumikas
    if($directory = $directory_no)
    If you are comparing numerically then you would use ==. If you are comparing a a string then use eq.

    [CODE=perl]if($directory eq $directory_no)[/CODE]

    perlop

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      A very common error to use the assignment operator "=" instead of a comparison operator "==":

      if ($directory = $directory_no)

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        [CODE=perl]@array = (1 .. 5);
        $n = 3;
        @new = grep {$_ >= $n} @array;
        print "@new";[/CODE]

        Comment

        • bhumikas
          New Member
          • Aug 2007
          • 13

          #5
          Originally posted by KevinADC
          [CODE=perl]@array = (1 .. 5);
          $n = 3;
          @new = grep {$_ >= $n} @array;
          print "@new";[/CODE]
          can u xplain the third line....

          Comment

          • Xmassey
            New Member
            • Oct 2007
            • 6

            #6
            @new = grep {$_ >= $n} @array;

            Only values that are more than or equal to $n (which is 3) are taken from @array and placed into the new array @new

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              perldoc: grep Function

              Comment

              • bhumikas
                New Member
                • Aug 2007
                • 13

                #8
                Hi,
                a small clarification,e ven if i put && <=$p its gonna evaluate rite as its an expression.

                Code:
                @new = grep {$_ >= $n && <= $p} @array;
                then it gonna put the values in between certain range into the new array...

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by bhumikas
                  Hi,
                  a small clarification,e ven if i put && <=$p its gonna evaluate rite as its an expression.

                  Code:
                  @new = grep {$_ >= $n && <= $p} @array;
                  then it gonna put the values in between certain range into the new array...
                  Yes, you can put any valid code into the expression thats evaluatued. Your code looks better writeen as:

                  Code:
                  @new = grep {$_ >= $n && $_ <= $p} @array;
                  of course $p needs to be defined.

                  Comment

                  • bhumikas
                    New Member
                    • Aug 2007
                    • 13

                    #10
                    Originally posted by KevinADC
                    Yes, you can put any valid code into the expression thats evaluatued. Your code looks better writeen as:

                    Code:
                    @new = grep {$_ >= $n && $_ <= $p} @array;
                    of course $p needs to be defined.

                    thnks for the suggestion.....

                    Comment

                    Working...