Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Something is defined when it shouldn't be?

by tnyflmngs (Acolyte)
on Nov 30, 2012 at 21:59 UTC ( #1006526=perlquestion: print w/replies, xml ) Need Help??
tnyflmngs has asked for the wisdom of the Perl Monks concerning the following question:

Hey folks, I have two pricing structures that I need to add to a price file prep process that I run in Perl. It seems like a perfectly simple thing to do, but I must not understand true and false like I should. In this price file there are around 150,000 lines and I am only interested in 4 of the columns in the tab delimited file. Those columns are the part number, part description, company 1 price, company 2 price. Here is the code:
my @line = split(/\t+?/, $row); my $part_number = $line[0]; my $part_desc = $line[1]; my $company1_price = $line[3]; my $company2_price = $line[5]; print Dumper $company1_price, $company2_price; if (!$company1_price && !$company2_price) { die "Entered if block\n"; #next; } else { die "Entered else block\n"; print $company1_file"$part_number\t$part_desc\t $company1_price\n"; print $company2_file "$part_number\t$part_desc\t $company2_price\n"; }
There are two cases I am looking for: 1. Both prices are empty or 2. Both prices are not empty. The two die statements are there for troubleshooting as is the print Dumper statement. If both prices have a price listed then I want to write the part number, part description, and price to the appropriate file for upload into the business system. The print Dumper statement prints the value of both prices as '     ' which I have visually verified, however I am entering the else block. Because of that I am writing every line to the file, which I don't want. What should I be using as a condition in the if block. Keep in mind that it is possible that a price value could look like '    150' and the number of white space characters is unknown.

Replies are listed 'Best First'.
Re: Something is defined when it shouldn't be?
by choroba (Bishop) on Nov 30, 2012 at 22:54 UTC
    ' ' is true in Perl. Only 0, '0', '', and undef are false (see What is true and false in Perl?). The word "defined" has a different meaning, too (see defined).

    If you are only interested in values that contain a digit, you can use a regular expression: /[0-9]/.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Something is defined when it shouldn't be?
by Kenosis (Priest) on Nov 30, 2012 at 23:34 UTC

    The following merely builds upon choroba's mentioning of using a regex to detect digits in the two price variables:

    use strict; use warnings; ... my ($part_number, $part_desc, $company1_price, $company2_price) = (spl +it /\t+/, $row )[0,1,3,5]; next unless $company1_price =~ /\d/ and $company2_price =~ /\d/; $company1_price =~ s/\s+//g; $company2_price =~ s/\s+//g; print $company1_file, "$part_number\t$part_desc\t$company1_price\n"; print $company2_file, "$part_number\t$part_desc\t$company2_price\n"; ...

    Additionally, both prices are cleared of any spaces, in case there are any.

Re: Something is defined when it shouldn't be?
by runrig (Abbot) on Dec 01, 2012 at 00:43 UTC
    What do you think the "+?" in /\t+?/ will do?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006526]
Approved by graff
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-03-18 16:38 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (230 votes). Check out past polls.