1753506 Members
6834 Online
108794 Solutions
New Discussion юеВ

Perl Help needed

 
SOLVED
Go to solution
Mike_Ca Li
Regular Advisor

Perl Help needed

I used perl script from forum: split.pl to generate 2 separate file under if-condition checking. The reason is I need to check region (TW/FR...) to recognize for production or testing. The new script can output 2 separate file but empty record. Can I use "$pname" for print statement. Please advice.


$onefile = shift or die " please provide filename in timestamp.filename format";
($uniq,$type)=split(/\./,$onefile);
$testfile = "testfile";
open (OLD,"<$onefile") or die " Could not open file: $onefile";
while () {
if (/^SWBAMSTW/) {
$count++;
$name = $uniq."-".$count.".".$type;
print "Prod: New split name $name\n";
open (NEW,">$name") or die "Prod: can not create $name";
$pname = "NEW";
}
if (/^SWBAMSFR/) {
$count++;
$name1 = $uniq."-".$count.".".$testfile;
print "Test : New split name $name1\n";
open (NEW1,">$name1") or die "Test: can not create $name1";
$pname = "NEW1";
}
die "no initial seperator found" unless ($count);
print $pname;
print "loop\n";
}


6 REPLIES 6
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl Help needed

using $pname won't work. This probably will

--8<---
my $onefile = shift or die " please provide filename in timestamp.filename format";
my ($uniq, $type) = split m/\./, $onefile, 2;
my $testfile = "testfile";
open my $old, "<$onefile" or die " Could not
my ($count, $outfile) = (0);
open file: $onefile";
while (<$old>) {
if (/^SWBAMSTW/) {
$count++;
$name = "$uniq-$count.$type";
print STDERR "Prod: New split name $name\n";
$outfile and close $outfile;
open $outfile, ">$name" or die "Prod: can not create $name";
}
if (/^SWBAMSFR/) {
$count++;
my $name = "$uniq-$count.$testfile";
print STDERR "Test: New split name $name1\n";
$outfile and close $outfile;
open $outfile, ">$name1" or die "Test: can not create $name";
}
$count or die "no initial seperator found";
print $outfile;
print STDERR "loop\n";
}
-->8---

HTH Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steven E. Protter
Exalted Contributor

Re: Perl Help needed

I would handle it thus(but i am not a maven).

two file handles

open (NEW1,">$name") or die "Prod: can not create $name";

open (NEW2,">$fname") or die "Prod: can not create $fname";


You have two file handles open and then you write conditionally to whichever file you need.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
H.Merijn Brand (procura
Honored Contributor

Re: Perl Help needed

And most (beginner) errors are caught and verbosely explained when sarting your script with

use strict;
use warnings;

and for the real unwary

use diagnostics;

this does the same as

# splain "error message"

Enjoy, Have FUN! H.Merijn [ who doen't know what a maven is :) ]
Enjoy, Have FUN! H.Merijn
Mike_Ca Li
Regular Advisor

Re: Perl Help needed

Hi:
I still don't understand the more than 1 file handle issue. I provide test input file. Pls advise. Thanks.
Log of a run:
Prod: New split name proto-1.txt
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
Test: New split name
Test: can not create proto-2.testfile at split2.pl line 20, <$old> line 12.
GLOB(0x225114)GLOB(0x225114)GLOB(0x225114)GLOB(0x225114)GLOB(0x225114)GLOB(0x225
114)GLOB(0x225114)GLOB(0x225114)GLOB(0x225114)GLOB(0x225114)GLOB(0x225114)
H.Merijn Brand (procura
Honored Contributor

Re: Perl Help needed

A slip of your^wmy pen. I left in your old naming in the open call here:

if (/^SWBAMSFR/) {
$count++;
my $name = "$uniq-$count.$testfile";
print STDERR "Test: New split name $name1\n";
$outfile and close $outfile;
open $outfile, ">$name1" or die "Test: can not create $name";
}

change $name1 to $name, and you should be fine.
Now insert those two 'use' statements in front of the script, and it would have clearly stated the error, I'm sure

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Miguel Covas
Advisor

Re: Perl Help needed

The safest way to assign a filehandle to a scalar is typeglobbing. Therefore you better use:

$pname=*NEW ;

and

$pname=*NEW1 ;

(But what you are doing must work except if you are using strict-refs)



Now, if you want to print something to a
file "print" must understand that you want that.

Use

print $pname $_ ;

With these minor changes I ran your script against your testfile, which downloaded as

112028.txt and obtained

112028-1.testfile (855 bytes)

112028-2.txt (2181 bytes)

112028-3.testfile (352 bytes)

112028-4.txt (472 bytes)

I suggest a visit to

http://www.perldoc.com/perl5.8.4/pod/perlfaq5.html