Re: Perl Help needed (140 Views)
Reply
Regular Advisor
Mike_Ca Li
Posts: 122
Registered: ‎03-13-2003
Message 1 of 7 (140 Views)
Accepted Solution

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";
}


Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 2 of 7 (140 Views)

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
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 3 of 7 (140 Views)

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
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 4 of 7 (140 Views)

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
Please use plain text.
Regular Advisor
Mike_Ca Li
Posts: 122
Registered: ‎03-13-2003
Message 5 of 7 (140 Views)

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)
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 6 of 7 (140 Views)

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
Please use plain text.
Advisor
Miguel Covas
Posts: 22
Registered: ‎05-21-2002
Message 7 of 7 (140 Views)

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

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation