A question on output of flip flop operator

J

justme

hi

i have a text file with some info like this:

[local]
/home/user
[files]
test1.txt
test1.htm
[dest]
/home/dest
[server]
127.0.0.1


i want to get the line between [local] and [files] so i did a small code

open(FILE , "<$input") or die "Cannot open $input for reading!: $!\n";
while(<FILE>)
{
print if (/\[local\]/ .. /\[files\]/ );

}


but i get the output :
[local]
/home/user
[files]

i only want to get "/home/user" and not the [local] and [files]
how can i do that?
thanks
 
P

phaylon

justme said:
i only want to get "/home/user" and not the [local] and [files] how can i
do that?

That's mentioned in »perldoc perlre«, look out for the ( .. ) brackets.
Should be enough examples in there. Also it would sure be a good idea to
take a complete look through the Perl documentation which you can call by
»perldoc perl«.

hth,
phay
 
A

A. Sinan Unur

(e-mail address removed) (justme) wrote in @posting.google.com:
Re: A question on output of flip flop operator

It is the print statement that generates the output.
i have a text file with some info like this:

[local]
/home/user
[files]
test1.txt
test1.htm
[dest]
/home/dest
[server]
127.0.0.1

It is preferable to put the data in the __DATA__ section of the scripts
you post so others can run your script with minimal effort. Please see
the posting guidelines for an explanation of this.
i want to get the line between [local] and [files] so i did a small
code

open(FILE , "<$input") or die "Cannot open $input for reading!: $!\n";

I tend to prefer lexical filehandles (because they are limited to the
current lexical scope instead of being global) and the three-argumen
form of open:

open my $file, '<', $input or die "Cannot open $input: $!";

The value of not suppressing the error line is debatable. It is probably
a good idea to keep it during development.
while(<FILE>)
{
print if (/\[local\]/ .. /\[files\]/ );

}

but i get the output :
[local]
/home/user
[files]

i only want to get "/home/user" and not the [local] and [files]
how can i do that?

One way would be to choose not to print [local] and [files]:

use strict;
use warnings;

my $start = '\[local\]';
my $end = '\[files\]';

while(<DATA>) {
if ( /$start/ .. /$end/ ) {
print unless /$start/ or /$end/;
}
}

__DATA__
[local]
/home/user
[files]
test1.txt
test1.htm
[dest]
/home/dest
[server]
127.0.0.1

__END__

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top