|
|
|
|
|
|
| Author |
Message |
justme *nix forums addict
Joined: 28 Mar 2005
Posts: 68
|
Posted: Tue Feb 08, 2005 2:18 pm Post subject:
A question on output of flip flop operator (part 2)
|
|
|
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"; |
|
| Back to top |
|
 |
phaylon *nix forums Guru Wannabe
Joined: 11 Apr 2005
Posts: 124
|
Posted: Tue Feb 08, 2005 2:48 pm Post subject:
Re: A question on output of flip flop operator (part 2)
|
|
|
justme wrote:
| Quote: | 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.
| Quote: | 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__
--
http://www.dunkelheit.at/
bellum omnium pater. |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Fri Jan 09, 2009 5:25 am | All times are GMT
|
|
MPAA | Advertising | PunBB forum hosting | McDonalds | Bankruptcy
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|