By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

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

100+
P: 170
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
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
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

100+
P: 170
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.