niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
A question on output of flip flop operator (part 2)
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
Author Message
justme
*nix forums addict


Joined: 28 Mar 2005
Posts: 68

PostPosted: Tue Feb 08, 2005 2:18 pm    Post subject: A question on output of flip flop operator (part 2) Reply with quote

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

PostPosted: Tue Feb 08, 2005 2:48 pm    Post subject: Re: A question on output of flip flop operator (part 2) Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 5:25 am | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm
No new posts mailq output format bacilko1 Postfix 0 Tue Oct 03, 2006 12:34 pm
No new posts configuration question for httpd Karl Wang Apache 1 Fri Jul 21, 2006 2:10 pm
No new posts nim problem/question Ron AIX 0 Fri Jul 21, 2006 1:57 pm
No new posts question for JAVA developer who r using postgres sql as b... deepak pal PostgreSQL 1 Fri Jul 21, 2006 9:00 am

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
[ Time: 0.1866s ][ Queries: 16 (0.1123s) ][ GZIP on - Debug on ]