Bobby wrote:
Here's a better excerpt of the script, see my comment towards the bottom. Thanks.
#!/usr/local/bin/perl
use strict;
open(IN, $ARGV[0]) || die "Could not open data file: $ARGV[0]\n";
my %US;
my %EURO;
my %ITAL;
my %GENERAL;
my $DEBUG = 0;
# map each store to its children stores
while (<IN>)
{
chomp;
my $line = $_;
# split rows into columns
my @fields = split /\|/, $line;
if($DEBUG) {
print "$line\n";
print "SKU:".$fields[0]."\n";
print " item_size ".$fields[19]."\n";
print " standard_size ".$fields[36]."\n";
print " us_sizes ".$fields[37]."\n";
print " us_sizes ".$fields[38]."\n";
}
# make sure base number field is numeric
if($fields[0] =~ /^[0-9]{5}/) {
# save the important fields
my $sku = $fields[0];
my $item_size = $fields[19];
my $standard_size = $fields[36];
my $size_type = $fields[37];
my $us_sizes = $fields[38];
next if($item_size eq ""); #if item size is note specified, skip it.
# push each store into array associated with its parent store
if($size_type eq "") {
$US{$item_size} = 1;
}
elsif($size_type eq "Euro") {
$EURO{$standard_size} = 1;
$EURO{$sku} => $sku;
}
elsif($size_type eq "Italian") {
$ITAL{$standard_size} = 1;
}
elsif($size_type eq "General") {
$GENERAL{$standard_size} = 1;
}
if($us_sizes ne "") {
$us_sizes =~ s/^,?(.*),?$/\1/g;
foreach my $size (split /,/, $us_sizes) {
#print "pushing found US size $size\n";
$US{$size} = 1;
}
}
}
}
close(IN);
printSizesXML();
sub xmlEscape
{
# STP: Change made to the Ampersand section, to &
my ($str) = @_;
$str =~ s/\&/\&/g;
$str =~ s/</\</g;
$str =~ s/>/\>/g;
#remove weird Unicode character
$str =~ s/\x10/ /g;
return $str;
}
sub in
{
my ($val, @list) = @_;
return (grep( /^$val$/, @list));
}
sub printSizesXML
{
my $level = 3;
my $spacer = " ";
my $n = 1;
foreach my $size ( sort keys %US )
{
next if($size !~ /^[0-9.]+$/);
$size = xmlEscape($size);
$n++
}
my $n = 1;
foreach my $size ( keys %EURO )
{
next if($size !~ /^[0-9.]+$/);
$size = xmlEscape($size);
print "$size|Euro\n";
#This is where i'm stuck
#I need to be able to print the $us_sizes from $fields[38]
#in the while loop above down in this foreach statement.
# The commented out code below is my failed attempt. Need some help here
#foreach my $sizeb ( sort keys %US ){
# if ($US{$sku} = $EURO{$sku}){
# print "$size Euro - $sizeb US\n";
# }
# }
$n++
}
Rodrick Brown <[EMAIL PROTECTED]> wrote: On Thu, May 22, 2008 at 12:39 PM,
Bobby wrote:
Hi all,
I have a flat file that contains a pid, us_size and euro_size. I want to create
read in the file and create one hash for the us_size (%US) and the other for
the euro_size (%EURO). Then i want to do a print statement if the pid value in
the us_size hash is equal to pid value in the euro_size hash...maybe IF
($US{$pid} = $EURO{$pid}) {print statement...}.
The part where i'm stuck on is how to assign the data into a hash and do the
comparison, could one of you help me with the Perl's syntax or point in the
right direction?
Thanks much!
pid|us_size|euro_size
1|10|34
2|11|35
3|12|37
4|13|
Why do you need a hash?
[EMAIL PROTECTED] ~]$ cat /tmp/out
1|10|34
2|11|35
3|12|37
4|13|13
[EMAIL PROTECTED] ~]$ perl -nle '($pid,$u,$e) = split/\|/,$_; print
"PID: $pid US: $u EU: $e" if ($u =~ m/$e/)' /tmp/out
PID: 4 US: 13 EU: 13
--
[ Rodrick R. Brown ]
It looks like you have a typo in you script
# The commented out code below is my failed attempt. Need some help here
#foreach my $sizeb ( sort keys %US ){
# if ($US{$sku} = $EURO{$sku}){
# print "$size Euro - $sizeb US\n";
should be
# if ($US{$sku} == $EURO{$sku}){
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/