
Laurent_R
Veteran
/ Moderator
Dec 16, 2012, 9:55 AM
Post #2 of 3
(2465 views)
|
Re: [Raju_P] Readig partial inormation from text file
[In reply to]
|
Can't Post
|
|
Of course you can. You would need to read each line individually and figure out whether you want to print it or not, depending on some conditions. For example, something like this:
my $select = $something; # something could be "#Menu1" or #Menu2 ENDFILE: while (my $line = <FILE>) { next while $line !~ /$select/; # discard lines until we arrive at the right menu option print $line; # print the menu option line while ($line = <FILE>) { # get tge following lines last ENDFILE if $line =~ /^#/; # end the loop if we get to a new menu option print $line; } } But this does not look too good and depends too much on assumptions made about the file format. A better option would be to define your menu file differently, for example something like this:
1:#Menu1 1:--------- 1:1. Option 1 1:2. Option 2 2:#Menu 2 2:---------- 2:1. option 1 2:2. Option 2 It is then cleaner, easier and far more robust to print only the relevant lines, depending on the menu number chosen, with something like this:
my $menu_number = $something; # something could be 1 or 2 while (my $line = <FILE>) { my @fields = split /:/, $line; print $fields[1] if $fields[0] == $menu_number; }
|