Print to new file for each results

PERL

    Next

  • 1. Embedding Perl in HTML
    I realize that this list may not be the best, or even most appropriate, place to ask this question, but I'm just so curious that I'm losing sleep over it! =;) What is the best way to embed perl in an HTML file that is _not_ located under the cgi-bin directory? I started learning PHP for this very reason, but I keep coming back to Perl. I've read some about Template Toolkit and others, but what I'm still confused over is if these templates _must_ be located in cgi-bin or can you embed code anywhere on your site? If possible, I would like to be able to embed perl, placeholders, templates, whatever, anywhere on my site. Perhaps I'm working under the wrong impression, but for some reason I'm thinking you can't. Hopefully I'm wrong. Any help in pointing me in the right direction will be greatly appreciated. Thanks, Marc

Re: Print to new file for each results

Postby mumia.w.18.spam+nospam » Thu, 31 Aug 2006 08:51:31 GMT



Although this is very different from your program, it should 
give you the general idea:

use strict;
use warnings;
use File::Slurp;
use File::Spec::Functions;

my @departments = q{
     192.168.14.0|88|Sales|Richard Throop
     192.168.16.0|90|Accounting|Juliet Rosenblatt
     192.168.20.0|91|Logistics|Mark Mane
     192.168.24.0|94|Security|Marlon Johnson
} =~ m/^\s*(.*)$/mg;

# print join "\n", @departments;
my $dir = catfile($ENV{HOME}, qw(tmp tmp));

foreach my $info (@departments) {
     my $fname = (split /\|/, $info)[2];
     $fname = catfile($dir, $fname);
     print $fname, "\n";
     write_file $fname, $info . "\n";
}



__HTH__
:-)


Print to new file for each results

Postby rmckeever » Thu, 31 Aug 2006 13:16:25 GMT

I am tring to use part of someones elses code that creates a data file which prints
out like this:

ip|result|deptA|data
ip|result|deptB|data
ip|result|deptC|data

My goal instead of having all the data in one big file is to loop this and create
a file for each result. 

So I would have a deptA.out file with deptA info then a deptB.out 
file with dept b info and etc.

This is not exactly doing what I need:

#!/usr/bin/perl 
use strict;
  
my %dept = ();
 
    while (<DATA>)
    {
        
 chomp $_;
 next if ($_ eq "");
 
my @lines = split(/[|]/);
 
#$line[3] = %dept++;

$dept{$lines[2]}++;

$dept{$lines} = $value;


 
#print "$line";
#foreach $line (@lines) {
#print $line."\n";
#}
  
    }
 
#print "@{[ %dept ]}\n";
#print "%dept"; 

my @array=keys(%dept);
print "All of the elements stored in %dept are @array \n";


#
 __DATA__
ip|result|deptA|data
ip|result|deptB|data
ip|result|deptC|data




How would I create a file for each hash then print to it??? Any help, thanks.


Thanks,
Ron


Re: Print to new file for each results

Postby krahnj » Thu, 31 Aug 2006 13:42:23 GMT



#!/usr/bin/perl
use warnings;
use strict;

my %filehandles;

while ( <DATA> ) {
    my $key = ( split /\|/ )[ 2 ];
    unless ( exists $filehandles{ $key } ) {
        open $filehandles{ $key }, '>', "$key.out"
            or die "Cannot open '$key.out' $!";
        }
    print { $filehandles{ $key } } $_;
    }

__DATA__
ip|result|deptA|data
ip|result|deptB|data
ip|result|deptC|data



John
-- 
use Perl;
program
fulfillment

Re: Print to new file for each results

Postby foskey » Thu, 31 Aug 2006 19:33:37 GMT



If you can ignore efficiency (not run very often...) then you can simply
reopen the file for each record.

while( <> ) {
  my @lines = split(/[|]/);
  my $file;

  open( $file, '>>', "$lines[1].out" ) or die...
  print $file $_;
  close( $file ) or die...;

}

-- 
Ken Foskey
FOSS developer


Similar Threads:

1.Searching text file and printing to new file

I'm a nearly absolute beginner to Perl, and a lot of the text
manipulation things confuse me. I have a large text file with
information essentially broken into lines like this:

findable text with a regexp
information I care about
more findable text

There are plenty of sections like this in the file. How can I write a
program that opens the file then searches for the middle line and
prints it to a new file?

2.Printing one txt file to a new txt file backwards

I need to print an entire text file to a new text file backwards.  I
have the following code, but it only prints the first line backwards,
and completely omits all the other lines of the original text file:

    open IN, "< $file1";
    open OUT, "> $file2";
    print OUT reverse split '', <IN>;
    close IN;
    close OUT;


Any help would be appreciated!

3.matching elements from array and print the results line by line from log file

Hi,
I have a list of element in array that I would like to match the
pattern in logs file. I have hard time take element from array and
matching it. More then weeks now and don't know where to find in man
page. please help me. Here is my code.

#!/usr/bin/perl -w
open  ( FILE, " /var/log/cisco.log " ) || die "can't open cisco.log
file!";
@cisco.log = <FILE>;
close (FILE);

@array = qw( BPDUGUARD FAN_FAIL ); # more then 2 elements in array

while (<@cisco.log>){
     foreach $line (@array){
     $_ =~ $line;      #DOESN'T WORK AND I AM STUCK..
       print "$_\n";       #WANT TO PRINT THAT MATCH
    }
}


__END__
thanks for your help

4.matching elements from array and print the results line by line from log file

slow_leaner wrote:
> Hi,

Hello,

> I have a list of element in array that I would like to match the
> pattern in logs file. I have hard time take element from array and
> matching it. More then weeks now and don't know where to find in man
> page.

perldoc -q "How do I efficiently match many regular expressions at once"

> please help me. Here is my code.
> 
> #!/usr/bin/perl -w
> open  ( FILE, " /var/log/cisco.log " ) || die "can't open cisco.log
> file!";
> @cisco.log = <FILE>;
> close (FILE);
> 
> @array = qw( BPDUGUARD FAN_FAIL ); # more then 2 elements in array
> 
> while (<@cisco.log>){

That is *not* how you read lines from a file.  Remove:

@cisco.log = <FILE>;
close (FILE);

from above and then your while loop should be:

while ( <FILE> ) {

>      foreach $line (@array){
>      $_ =~ $line;      #DOESN'T WORK AND I AM STUCK..

You are performing the match in void context so it is superfluous.  See 
the FAQ entry above for how to do it.

>        print "$_\n";       #WANT TO PRINT THAT MATCH
>     }
> }


John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

5.XML::Twig print to file gives expected results except for failure return value

(I had some conversation in c.l.p.m about this originally but it's no
longer a Perl coding problem so I'm moving it here.)

Using XML::Twig I'm reading in an XML file, adding an element to it
and writing the result out again.  The file is correctly written, but
the return code from the "print" method denotes failure.  The Twig
documentation doesn't indicate that behaviour; can someone help me
debug this further?

--------- code to show the error ----------
use strict;
use warnings;

use XML::Twig;

# Get the XML file
my $fdtwig = new XML::Twig;
$fdtwig->parsefile('F:\p\XMLin.XML');
my $fd = $fdtwig->root;

# Create a new twig and paste it into the XML doc
my $stuff = "somestuff";
my $elt = new XML::Twig::Elt('stuff',$stuff);
$elt->paste('last_child',$fd);

# Write the modified XML document to a new file
open XMLOUT, '>F:\p\xmlout.xml';
$fdtwig->print(\*XMLOUT) or
	die "Failed to write modified XML file:$!\n";

close XMLOUT;
------------------------------

The result of this (running with ActiveState Perl 5.8.2) is

  F:\p>perl tryit.pl
  Failed to write modified XML file:Bad file descriptor

But the file "xmlout.xml" is correctly created with the expected
contents.  I debugged the program and verified that the return value
is "false".

Henry Law       <><     Manchester, England 

6. new line "\n" for print file

7. To print the unmatched line into a new file

8. Printing Hyperlink results from Access form?



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 73 guest