- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Perl scripting
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 04:23 PM
тАО07-27-2004 04:23 PM
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 $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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 08:50 PM
тАО07-27-2004 08:50 PM
SolutionGetting 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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 09:01 PM
тАО07-27-2004 09:01 PM
Re: Perl scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2004 12:32 PM
тАО07-28-2004 12:32 PM
Re: Perl scripting
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2004 01:14 PM
тАО07-28-2004 01:14 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2004 01:31 PM
тАО07-28-2004 01:31 PM
Re: Perl scripting
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2004 02:28 PM
тАО07-28-2004 02:28 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2004 05:24 PM
тАО07-28-2004 05:24 PM
Re: Perl scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2004 05:28 PM
тАО07-28-2004 05:28 PM
Re: Perl scripting
my $fc_name = "fileC";
open (my $fc, $fc_name) or die "$fc_name: $!";
while (<$fc>) {
}
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-29-2004 05:43 AM
тАО07-29-2004 05:43 AM
Re: Perl scripting
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',