LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-29-2011, 01:44 PM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Rep: Reputation: 2
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

Last edited by PoleStar; 03-29-2011 at 01:57 PM.
 
Old 03-29-2011, 02:21 PM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
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.
 
Old 03-29-2011, 02:24 PM   #3
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
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
 
Old 03-29-2011, 02:32 PM   #4
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
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 ?
 
Old 03-29-2011, 02:58 PM   #5
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
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
 
Old 03-29-2011, 03:06 PM   #6
toordog
Member
 
Registered: Jul 2010
Location: Montreal, Canada
Distribution: RedHat, Ubuntu, Solaris, AIX, BSD
Posts: 42

Rep: Reputation: 3
Quote:
Originally Posted by PoleStar View Post
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
}

Last edited by toordog; 03-29-2011 at 03:17 PM.
 
Old 03-29-2011, 07:13 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl grep statement hattori.hanzo Programming 2 11-19-2010 05:01 AM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 05:50 AM
Perl if statement issue shriyer Linux - Software 4 08-13-2009 08:07 PM
Perl : if statement and RE's PB0711 Programming 4 02-04-2006 08:01 PM
Help in PERL with an IF statement! richhoward Programming 5 06-13-2003 01:30 PM


All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration