
1arryb
User
Feb 25, 2009, 11:42 AM
Post #4 of 8
(529 views)
|
|
Re: [Xer()] Problem with writing to a file with specific conditions
[In reply to]
|
Can't Post
|
|
Xer(), If this won't work, I still don't understand your question:
#!/usr/bin/perl use strict; use warnings; my $outFile = $ARGV[0]; open (OUT, "> $outFile") or die "Can't open $outFile for writing."; my @lines = (); # Populate @lines somehow. This is just test data. @lines = ( '98 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=N', '19 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=Y', '5 name=XXXXXX state=CP_IDLE mod=N confnum=0 vll=Y' ); # Array of 'vll=y' icns for later output. # NOTE: If icn repeats within @lines, you'll have to change # @icns to a hash in order to avoid duplicates in the output: # my %icns = (); # ... # $icns->{$icn} = 1; # ... # map { print OUT "$_ Y\n" } (sort { $a <=> $b } (keys(%icns)); my @icns = (); # Process @lines records for my $line (@lines) { # Get rid of the line termination. chomp($line); # Split the line in to records on spaces. my @rec = split(/ /, $line); my $icn = $rec[0]; # Get the part after 'vll='. my $vll = (split(/=/, $rec[5]))[1]; # Discard unwanted records. next unless $vll eq 'Y'; # Store the 'vll=y' icns we'll output. push(@icns, $icn); } # Output in icn-sorted order. map { print OUT "$_ Y\n" } (sort { $a <=> $b } @icns); # Fixed! close OUT; Cheers, Larry
(This post was edited by 1arryb on Feb 25, 2009, 3:18 PM)
|