Re: A question on output of flip flop operator (part 2)
From: phaylon (phaylon_at_dunkelheit.at)
Date: 02/08/05
- Next message: John W. Kennedy: "Re: FAQ 4.16 How can I find the Julian Day?"
- Previous message: ioneabu_at_yahoo.com: "Re: copy directory structure without files"
- In reply to: justme: "A question on output of flip flop operator (part 2)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 08 Feb 2005 16:48:59 +0100
justme wrote:
> 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__
-- http://www.dunkelheit.at/ bellum omnium pater.
- Next message: John W. Kennedy: "Re: FAQ 4.16 How can I find the Julian Day?"
- Previous message: ioneabu_at_yahoo.com: "Re: copy directory structure without files"
- In reply to: justme: "A question on output of flip flop operator (part 2)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|