A question on output of flip flop operator (part 2)

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
[end]

[local]
/home/user1
[files]
test1.dat
test2.txt
[dest]
/home/dest1
[server]
127.0.0.1
[end]

Actually i am doing an ftp configuration file where the perl sript
will go through this file and pick up the definitions between [local]
and [end]
and then using Net::FTP module to transfer files from local directory
to destination at server defined by [server] until [end] and then
start transferring the next section from [local] to [end]

Am i going the correct way by doing the incomplete code below ? If not
any better way to do that...thanks

while(<FILE>)
{
if (/\[local\]/ .. /\[files\]/ ) {
next if ( $_ =~ /\[local\]/ or $_ =~ /\[files\]/ ) ;
$localdir = $_; #get the line after [local]
}
if ( /\[files\]/ .. /\[dest\]/ )
{
print "files = $_\n";
next if ( $_ =~ /\[files\]/ or $_ =~ /\[dest\]/ );
push(@files, "$_");
}
if (/\[dest\]/ .. /\[server\]/ )
{
next if ( /\[dest\]/ or $_ =~ /\[server\]/ );
$dest = $_; #get dest directory to put @files
}

.....
......
}

print "local is $localdir\n";
print "files are = @files\n";
print "dest is = $dest\n";
 
P

phaylon

justme said:
i have a text file with some info like this:

Why are you opening another thread? I've got your last one facing this
topic right on second line.
Am i going the correct way by doing the incomplete code below ? If not
any better way to do that...thanks

It is *a* way to do that[1]. But you have to keep it exactly in this
order. Another approach may be to use the range operator (C<..>) to find
out if you are in the local/end section and then process each line.
Below's a quick example which also builds a little data structure you may
find useful.

hth,
phay

( [1] I haven't read your code complete, so I'm just talking about the
*way of doing it.* )

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my(@data, $section);
my $count = 0;

line: while( <DATA> ) {
if( /^\[local\]$/i .. /^\[end\]$/i ) {
chomp;

#-- skip empty lines
next line if /^\s*$/;

#-- find current section
$section = $1 and next line
if /^\[(files|dest|server|local|end)\]$/i;

#-- end means, we're out of any section, next line
next line if $section eq lc 'end';

#-- everything but files seems to be one-value
$data[ $count ]{ $section } .= $_
and next line
unless $section eq lc 'files';

#-- push files entry
push @{$data[ $count ]{ $section }}, $_;
}
else {
$count++;
}
}
print Dumper \@data;

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

[local]
/home/user1
[files]
test1.dat
test2.txt
[dest]
/home/dest1
[server]
127.0.0.1
[end]
__END__
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top