Home > Archive > PERL Beginners > February 2007 > print if variable matches key
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
print if variable matches key
|
|
| Brian Volk 2007-02-28, 6:59 pm |
| Hello,
I'm trying to print a tab delimited file based on a colon separated file
matching a key in %hash. I've attempted a few different things w/ the
colon separated file (splitting it into to variables, making it %hash2)
but I'm not having any luck.
I'm trying to match up a text file, created from an email form, into the
correct tabbed column. The %hash contains all the fields possible that
could come over in the emailed form... the email (text file) only
contains fields that were completed.
Text file (comma separated)
VendName: vendors name
VendEmail: vendors@example.com
VendPhone: 555-555-5555
VendWeb: www.example.com
CSManName: Customer Service Name
%hash - values all = 1
VendName
VendMailAdd
VendCSZ
VendEmail
VendPhone
VendFax
VendWeb
MinorityNo
CSManName
Below is what I have so far.
#!/usr/bin/perl
use strict;
use warnings;
my $file = "c:/brian/AX/vendor/vendor_fields.txt"; # testing... works!
open(FILE, $file) or die "Can't open the $file: $!\n";
my $new_ax = "c:/brian/AX/vendor/vendor_fields_new.txt";
open (NEW, "> $new_ax") or die "Can't open $new_ax:$!\n";
my %hash;
while(<FILE> ){
chomp $_; #remove the newline
my($key,$value) = split(/\t/,$_); #split by tab
$hash{$key} = $value; #assign the value here
my $email = "c:/brian/AX/vendor/email_file_new.txt";
open(EMAIL, $email) or die "Can't open the $email: $!\n";
while(<EMAIL> ){
chomp $_;
my($emailField, $emailValue) = split(/\:/,$_);
if ($emailField eq $key) {
print NEW "$emailValue \t";
} else {
print NEW "blank \t";
}
}
}
Any help would be greatly apprecieated.
Thanks!
Brian Volk
| |
| usenet@DavidFilmer.com 2007-02-28, 6:59 pm |
| On Feb 28, 10:34 am, B...@HPProducts.com (Brian Volk) wrote:
> if ($emailField eq $key) {
This is your main problem. $key is the last key you evaluated. I
think you want to see if any hash element has the key $emailField, so
you would do:
if ( $hash{$emailField} ) {
--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)
| |
| Brian Volk 2007-02-28, 6:59 pm |
|
> -----Original Message-----
> From: Brian Volk [mailto:BVolk@HPProducts.com]
> Sent: Wednesday, February 28, 2007 1:34 PM
> To: beginners@perl.org
> Subject: print if variable matches key
>
> Hello,
>
>
>
> I'm trying to print a tab delimited file based on a colon separated file
> matching a key in %hash. I've attempted a few different things w/ the
> colon separated file (splitting it into to variables, making it %hash2)
> but I'm not having any luck.
>
>
>
> I'm trying to match up a text file, created from an email form, into the
> correct tabbed column. The %hash contains all the fields possible that
> could come over in the emailed form... the email (text file) only
> contains fields that were completed.
>
>
>
[Brian Volk] Sorry to respond to my own post... but I think I'm getting
closer... The file is printing okay...I just need to know how to print a tab
if the $emailField is not in <EMAIL> ...I guess that is the tricky part. :-)
I should probably sort to keep the same field order...
[Brian Volk]
my $email = "c:/brian/AX/vendor/email_file_new_less.txt";
open(EMAIL, $email) or die "Can't open the $email: $!\n";
while(<EMAIL> ){
chomp $_;
my($emailField, $emailValue) = split(/\:/,$_);
if (exists $hash{$emailField}) {
print NEW "$emailValue \t";
} else {
print NEW "blank \t";
}
}
Any pointers would be greatly appreciated.
Thanks!
Brian
| |
| Wagner, David --- Senior Programmer Analyst --- WG 2007-02-28, 6:59 pm |
| > -----Original Message-----
> From: Brian Volk [mailto:bvolk@bvolk.com]=20
> Sent: Wednesday, February 28, 2007 12:24
> To: Brian Volk; beginners@perl.org
> Subject: RE: print if variable matches key=20
>=20
>=20
>=20
> separated file
> things w/ the
> making it %hash2)
> form, into the
> possible that
> [Brian Volk] Sorry to respond to my own post... but I think=20
> I'm getting
> closer... The file is printing okay...I just need to know how=20
> to print a tab
> if the $emailField is not in <EMAIL> ...I guess that is the=20
> tricky part. :-)
> I should probably sort to keep the same field order...=20
> [Brian Volk]=20
>=20
> my $email =3D "c:/brian/AX/vendor/email_file_new_less.txt";=20
> open(EMAIL, $email) or die "Can't open the $email: $!\n";
>=20
> =20
> while(<EMAIL> ){
> chomp $_;
> my($emailField, $emailValue) =3D split(/\:/,$_);
> if (exists $hash{$emailField}) {=20
> print NEW "$emailValue \t";
> } else {
> print NEW "blank \t";
> } =20
> }
>=20
> Any pointers would be greatly appreciated.
>=20
> Thanks!
>=20
> Brian=20
>=20
I am jusmping into the middle, so not real certain on what the
output looks like, but the problem comes from what a\t does under the
current shell setup you are using. If you print $emailValue and it is 20
characters plus \t will not look the same if you print 5 characters plus
a \t. You could use printf and then size appropriately and \t will do
what you want.
If you have any problems or questions, please let me know.
Thanks.
Wags ;)
David R Wagner
Senior Programmer Analyst
FedEx Freight
1.408.323.4225x2224 TEL
1.408.323.4449 FAX
http://fedex.com/us=20
=20
****************************************
******************************
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
****************************************
******************************
| |
| Brian Volk 2007-02-28, 6:59 pm |
|
> -----Original Message-----
> From: Wagner, David --- Senior Programmer Analyst --- WGO
> [mailto:David.Wagner@freight.fedex.com]
> Sent: Wednesday, February 28, 2007 3:31 PM
> To: Brian Volk; Brian Volk; beginners@perl.org
> Subject: RE: print if variable matches key
>
> I am jusmping into the middle, so not real certain on what the
> output looks like, but the problem comes from what a\t does under the
> current shell setup you are using. If you print $emailValue and it is 20
> characters plus \t will not look the same if you print 5 characters plus
> a \t. You could use printf and then size appropriately and \t will do
> what you want.
>
> If you have any problems or questions, please let me know.
>
> Thanks.
>
> Wags ;)
[Brian Volk] thank you for the response...actually I'm printing the output
to a tab file so excel handles my needs fine. I'm just having trouble
"printing" a tab when the key does not exist in %hash. If I can get this to
work, ...then every time I run the program on a new email file it will place
the record in the correct field in the existing file. (I'm guessing...but
one thing at a time for me! :-) )
Brian
|
|
|
|
|