468,316 Members | 2,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,316 developers. It's quick & easy.

how do i go onto the next line of data in a text file?

170 100+
hi guys,

i got the info say

CLASS 1234
NAME DAN


if i use the code below:
Expand|Select|Wrap|Line Numbers
  1.  
  2. if ($data1 =~ /CLASS/)
  3.  
  4.    my $alarm_class_line = index ($data1, "CLASS");
  5.    my $alarm_class = substr ($data1, ($alarm_class_line+6),4);
  6.    print "$alarm_class  ";
  7.  
it will print out 1234

but how do i indicate the next line and print out DAN?
cos the matching only match line by line..
not sure how i can go onto the next line?
Jun 2 '08 #1
2 1074
nithinpes
410 Expert 256MB
hi guys,

i got the info say

CLASS 1234
NAME DAN


if i use the code below:
Expand|Select|Wrap|Line Numbers
  1.  
  2. if ($data1 =~ /CLASS/)
  3.  
  4.    my $alarm_class_line = index ($data1, "CLASS");
  5.    my $alarm_class = substr ($data1, ($alarm_class_line+6),4);
  6.    print "$alarm_class  ";
  7.  
it will print out 1234

but how do i indicate the next line and print out DAN?
cos the matching only match line by line..
not sure how i can go onto the next line?
There are many ways of doing it. If you are storing the contents of file into an array, the following method would be better.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. open (FILE, "data.txt");
  4.  
  5. my @file=<FILE>;
  6. for my $i (0...$#file) {
  7.     chomp;
  8.   if ($file[$i] =~ /CLASS/)  {
  9.    $i++;
  10.    print "$1\n" if($file[$i]=~/\S+\s+(\S+)/);
  11. }
  12.  
  13. }
  14. close(FILE);
  15.  
Jun 2 '08 #2
poolboi
170 100+
hm...so when u do a i++, it's actually going on to the next line right??
alright i will try that

'cos currently i;m using foreach
i shall show u part of my code
Expand|Select|Wrap|Line Numbers
  1. foreach $data1 (@data)
  2. {
  3. chomp ($data1); 
  4.  
  5. if ($data1 =~ /DISTUR/ && $data1 !~ /DISTURBANCE/)
  6. {
  7.    print "DISTUR  ";
  8.  
  9.    my $alarm_class_line = index ($data1, "DISTUR");
  10.    my $alarm_class = substr ($data1, ($alarm_class_line-4),3);
  11.    print @array1, "$alarm_class";
  12.  
  13.    my $alarm1 = substr ($data1, ($alarm_class_line+7),5);
  14.    print @array2, "$alarm1";
  15.  
  16.    my $alarm2 = substr ($data1, ($alarm_class_line+18),9);
  17.    print @array3, $alarm2;
  18.  
  19.  
  20. }
  21.  
i still got other arrays cos i'm gonna put them into my different fields in mySQL database

and i came across a row like the example i show about printing DAN
cos i dun wanna do the tedious matching at the beginning of the sentence
thought i could just do a go on to the next line thing

hm..not sure if i can implement it in foreach but i will try and see if adjustment can be made
Jun 2 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

19 posts views Thread by les_ander | last post: by
1 post views Thread by Colin Green | last post: by
2 posts views Thread by continium | last post: by
8 posts views Thread by Daz | last post: by
reply views Thread by Semajthewise | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.