1821980 Members
3046 Online
109638 Solutions
New Discussion юеВ

Perl scripting

 
SOLVED
Go to solution
Ratzie
Super Advisor

Perl scripting

I had great replies that have fixed my errors in my bad scripting of perl. (Thank you Rodney and Procura!). So I thought I should post new, since it is about new ideas that I want my script to do.

I would like when the user enters in the building to save the first 4 chars of that word to a variable. This I will use in naming the files later on.


Here is the script...
#!/opt/perl/bin/perl

use strict;
use warnings;

system ("clear"); #Clear the screen
my $acode = 204;

print "Enter BLD >";
chomp (my $bld = );
print "Enter room>";
chomp (my $room = );

open (inp, "open my $fc, ">fileC" or die "fileC: $!";
open my $ft, ">fileT" or die "fileT: $!";
open my $fo, ">fileO" or die "fileO: $!";
while () {
chomp; # Will remove the leading , or new line
my @a = split /,/, $_, -1;
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f join "," => $acode.$a[0], $bld, $room, $a[1], $a[2], "\n";
}


Second, I need to manipulate one of the files (fileC). Do I need to open this file, or is it still open? I want to pull the first column and surround the text with ' '. (This will be used in an sql query)
Do I need to assign it to an array? I would like to print it to screen first, how would I do that.

My further quest would be to call an sql query with the data that is in that file after it has been manipulated.

Would I then call an script.sql thru perl, that will used fileC-1 for it's data, or can it be all done thru perl. I have never seen that before.
9 REPLIES 9
Fred Ruffet
Honored Contributor
Solution

Re: Perl scripting

Hi !

Getting the first 4 chars of a string :
$> perl
$String="foobar";
$SubString=substr($String,0,3);
print "$SubString\n";
foo

At end of your script, your files are still open. You do not used close. That's bad :)
Anyway, if you want to update your file, opening mode isn't good (output). What I would do is to reopen fileC for read and write to another file, adding "'".
close $fc;
close $ft;
close $fo;
$fc="fileC";
$fc2=">newfileC";
open fc or die "fileC : $!";
open fc2 or die "newfileC : $!";
while () {
chomp;
($FirstField,@Rest)=split /,/;
print fc2 join ",","'$FirstField'",@Rest;
}
close fc;
close fc2;

To use SQL through perl, you'll have to use DBI and a DBD module. DBI is a common interface to Databases. It will access a DBD module that matches the DB you want (Oracle, MySQL...). Once downloaded the modules (http://cpan.org) and installed, you'll have good usage examples through perldoc.

Regards,

Fred


--

"Reality is just a point of view." (P. K. D.)
H.Merijn Brand (procura
Honored Contributor

Re: Perl scripting

Not an answer to your new question, but there still is some `typo' in the script

print $f join "," => $acode.$a[0], $bld, $room, $a[1], $a[2], "\n";

print $f join "," => $acode, $a[0], $bld, $room, $a[1], $a[2], "\n";

Which is *almost* the same. The first line will remove the ',' between $acode and $a[0]

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ratzie
Super Advisor

Re: Perl scripting

I was hoping someone can explain the errors that I am getting when I attach the rest of the script...
close $fc;
close $ft;
close $fo;
$fc="fileC";
$fc2=">newfileC";
open fc or die "fileC : $!";
open fc2 or die "newfileC : $!";
while () {
chomp;
($FirstField,@Rest)=split /,/;
print fc2 join ",","'$FirstField'",@Rest;
}
close fc;
close fc2;


These are the errors I get.
Unquoted string "fc" may clash with future reserved word at ./test.pl line 31.
Unquoted string "fc" may clash with future reserved word at ./test.pl line 38.
Global symbol "$fc2" requires explicit package name at ./test.pl line 30.
Global symbol "$FirstField" requires explicit package name at ./test.pl line 35.
Global symbol "@Rest" requires explicit package name at ./test.pl line 35.
Global symbol "$FirstField" requires explicit package name at ./test.pl line 36.
Global symbol "@Rest" requires explicit package name at ./test.pl line 36.
Execution of ./test.pl aborted due to compilation errors.
Hein van den Heuvel
Honored Contributor

Re: Perl scripting



That code morcel actually works without errors on my box. We'd need more context to understand the line numbers. Attach the whole file if you keep having problems?

I don't like several parts.
Here are a few comments.

>> open fc or die "fileC : $!";

I'd make that: open (fc, $fc) or die "$fc : $!";

This will clearly identify 'fc' as a file handle and remove 'potential confusion with reserved words'.
And by using the file name variable string in the 'die' text, you will avoid a future dis-connect between name reported and actuall name used.

Ditto for fc2

>>> join ",","'$FirstField'",@Rest;

How about some parens to make clear (for yourself!) what is being joined, what is printed. Because you 'chomped' you'll probably need to glue on a new-line.
So try something like:

print fc2 join (",","'$FirstField'",@Rest)."\n" ;

Good luck!

Hein.


Ratzie
Super Advisor

Re: Perl scripting

I am still getting errors...
I appreciate all the help...
PS: How do I save the first 4 char in an STDIN?
print "Enter BLD >";
chomp (my $bld = );

I need the first 4 char of what they typed.

**********************
#!/opt/perl/bin/perl
use strict;
use warnings;

system ("clear"); #Clear the screen
my $acode = 204;

print "Enter BLD >";
chomp (my $bld = );
print "Enter room>";
chomp (my $room = );

open my $fc, ">fileC" or die "fileC: $!";
open my $ft, ">fileT" or die "fileT: $!";
open my $fo, ">fileO" or die "fileO: $!";

while (<>) {
chomp; # Will remove the leading , or new line
my @a = split /,/, $_, -1;
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f join "," => $acode.$a[0], $bld, $room, $a[1], $a[2], "\n";
}


close $fc;
close $ft;
close $fo;

$fc="fileC";
$fc2=">newfileC";
open (fc, $fc) or die "$fc: $!";
open (fc2, $fc2) or die "$fc2 : $!";
while () {
chomp;
($FirstField,@Rest)=split /,/;
print fc2 join (",","'$FirstField'",@Rest)."\n";
}
close fc;
close fc2;

****************************
Unquoted string "fc" may clash with future reserved word at ./test.pl line 32.
Variable "$fc2" is not imported at ./test.pl line 33.
Unquoted string "fc" may clash with future reserved word at ./test.pl line 39.
Global symbol "$fc2" requires explicit package name at ./test.pl line 31.
Global symbol "$fc2" requires explicit package name at ./test.pl line 33.
Global symbol "$fc2" requires explicit package name at ./test.pl line 33.
Global symbol "$FirstField" requires explicit package name at ./test.pl line 36.
Global symbol "@Rest" requires explicit package name at ./test.pl line 36.
Global symbol "$FirstField" requires explicit package name at ./test.pl line 37.
Global symbol "@Rest" requires explicit package name at ./test.pl line 37.
Execution of ./test.pl aborted due to compilation errors.


/opt/perl/bin/perl -v

This is perl, v5.8.2 built for PA-RISC1.1-thread-multi
(with 25 registered patches, see perl -V for more detail)

Hein van den Heuvel
Honored Contributor

Re: Perl scripting


Ok, the problem now is that you learned to use 'use strict' only half way.
IF you use it, then you must stricly declare your variables. So you would need to 'my' this $FirstField and @Rest.
(or just drop the 'use strict' for a qucik test. Did I say that? I did not say that!)

Next, you effectively declared $fc as a file handle in the first open, and then you treat it like a string in the assignment '$fc="fileC";

So you need a fresh variable for that.
For example (just an example to make it clear, not to follow verbatim):

$fc_name="fileC";
open (FC_handle, $fc_name) or die "$fc_name: $!";
while () ...


So much to learn... :-).

Hein.


H.Merijn Brand (procura
Honored Contributor

Re: Perl scripting

first 4 characters of input

chomp (my $bld = );
length ($bld) > 4 and $bld = substr $bld, 0, 4;

or

$bld =~ s/^(....).*/$1/;

or

length ($bld) > 4 and $bld = join "", (split //, $bld, -1)[0..3];

or

use your imagination ...

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Perl scripting

Oh, and as of - I think - 5.8.0 file handles can (and imho should) be used as lexical variables, which makes it much easier to pass around and protect from invalid access

my $fc_name = "fileC";
open (my $fc, $fc_name) or die "$fc_name: $!";
while (<$fc>) {
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ratzie
Super Advisor

Re: Perl scripting

WOW, this perl is not user friendly. I really have to fight to understand it. Thank you for the help.
I have been able to my script to work with out errors but I can not pull the first column of data out of the file. It put the same thing into the fc2 as fc had.

...
my $fc_name = "fileC";
open (my $fc, $fc_name) or die "$fc_name:$!";
open my $fc2, ">fileC2" or die "fileC2: $!";
while (<$fc>) {
chomp;
my ( $FirstField,@Rest)=split /,/;
print $fc2 join (",","'$FirstField'",@Rest)."\n";
}
close fc;
close fc2;

This is fc
'204xxxx345',BUILDING2,ROOM3,00 0 10 14,CUSTOMER HAS

I need this in fc2
'204xxxx345',