LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   perl statement help (https://www.linuxquestions.org/questions/linux-newbie-8/perl-statement-help-871756/)

PoleStar 03-29-2011 01:44 PM

perl statement help
 
Maybe I am too tired, but is there any thing wrong with this statement ?

Code:


$i = 0;

while(<FILE>){

        print "$i\n";
        if ($i == 10){
              $i = [$i + 12];
        }
        $i++;

}

it brings back

Code:


0
1
2
3
4
5
6
7
8
9
10
159283633
159283634

in stead of

8
9
10
13
14

Thanks

savona 03-29-2011 02:21 PM

Maybe if you explained what your trying to accomplish here?

If your trying to count the lines in the file there are easier ways to do it.

savona 03-29-2011 02:24 PM

Maybe this?

#!/usr/bin/perl

$i = 0;
open (MYFILE, 'test');
while(<MYFILE>){
print "$i\n";
$i++;
}
close(MYFILE);

outputs:

$ ./add
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

PoleStar 03-29-2011 02:32 PM

Ok the script I am working on is supposed to insert image in excel sheet, so once it insert
some thing on one point I want it to add 12 in the count before it continue its operation(adding data in the execl)

like

1-data
2-data
3- image-start
4
5
6
7
8
..
15-image-finish
16-data
17-data



So, why isn't my code adding 12 into $i ?

savona 03-29-2011 02:58 PM

Syntax error:)

#!/usr/bin/perl
$i = 0;
open (MYFILE, 'test');
while(<MYFILE>){
print "$i\n";
if ($i == 10){
$i = $i + 12;
}
$i++;
}
close(MYFILE);

outputs:

$ ./add
0
1
2
3
4
5
6
7
8
9
10
23
24
25
26
27
28
29
30
31
32
33

toordog 03-29-2011 03:06 PM

Quote:

Originally Posted by PoleStar (Post 4307723)
Code:


$i = 0;

while(<FILE>){

        print "$i\n";
        if ($i == 10){
              $i = [$i + 12];
        }
        $i++;

}


Right now, your code loop the file (dunno what is your file so let assume it's a text file)
as long as it loop, you print $i (before incrementing) and you increment it by 1 until it reach 10. So it will not add 12 at the count of 4. Once you reach 10 it become $i become 22 and is increment by 1 to become 23 and 23 is print. Since i is still bigger than 10, it's being add 12 so 35 + 1 and 36 is displayed and so one.


Let assume you want to follow the schema you wrote.

Code:

$i = 1;
$k = 1;

while(<FILE>){
       
        //I don't know the perl syntax, take it for the logic only, syntax is Shell for the FOR LOOP
        for k in 1 2 3
        do
        print "$i\n";
        $k++;
        $i++;
        done
        $i=[$i +12]
        $k=1
}


chrism01 03-29-2011 07:13 PM

As Savona pts out, there's a syntax error; square brackets indicate an array.
You should ALWAYS (Perl Best Practice) start a Perl prog
Code:

#!/usr/bin/perl -w
use strict;            # Enforce declarations

An alternate to the -w switch is 'use warnings;'.
These 2 cmds will save you a heap of trouble, trust me.
Also, because Perl is heavily C derived, you could increment the '12' like this
Code:

$i+=12;
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials


All times are GMT -5. The time now is 02:59 AM.