problem with if-else assignment in text database

V

Vumani Dlamini

I am trying to extract the value of the number of males employed by a
company and used the expression below: The file contains data on
several companies. I would then like to store the variable with the
same length. Thus I would like to pad values below "10" by "0". The
problem is that some field have "00" for the number of males employed,
thus I try to test whether all two positions are filled before the
assignment using the expression below, but it seemingly doesn't work,


elsif (/EMPmale=(\S+)/) {
if ($1 == /\S{2}/) {$Emale = $1 ;}
else {$Emale = "0$1" ;}
}

I have also tried,

elsif (/EMPmale=(\S+)/) {
$Emale = $1 if $1 == /\S{2}/ ;
$Emale = "0$1" if $1 == /\S/ ;
}

But it also does not seem to give what I expect. Hope you girls (and
possibly guys) help me as always.


Vumani
 
G

Gunnar Hjalmarsson

Vumani said:
I am trying to extract the value of the number of males employed by
a company and used the expression below: The file contains data on
several companies. I would then like to store the variable with the
same length. Thus I would like to pad values below "10" by "0".
The problem is that some field have "00" for the number of males
employed, thus I try to test whether all two positions are filled
before the assignment using the expression below, but it seemingly
doesn't work,

elsif (/EMPmale=(\S+)/) {
if ($1 == /\S{2}/) {$Emale = $1 ;}
else {$Emale = "0$1" ;}
}

You are making at least three mistakes:

1. On the second of the above lines you are using the equality
operator '==' instead of the binding operator '=~'.

2. If you had had warnings enabled, Perl would have called your
attention to problem # 1. (So not using warnings was the second
mistake. ;-) )

3. $1 contains what was captured in the last run regex. Consequently,
when doing
$Emale = $1 ;
on the second line, $1 is empty, since nothing was captured in the
last run regex (which is on the same line).

This is one way to do it:

elsif (/EMPmale=(\S+)/) {
if (length $1 == 2) {$Emale = $1 ;}
else {$Emale = "0$1" ;}
}

and this is another way:

elsif (/EMPmale=(\S+)/) {
$Emale = sprintf '%02d', $1;
}
 
A

A. Sinan Unur

(e-mail address removed) (Vumani Dlamini) wrote in

Your subject line is not very relevant to the question you are asking.
I am trying to extract the value of the number of males employed by a
company and used the expression below: The file contains data on
several companies. I would then like to store the variable with the
same length. Thus I would like to pad values below "10" by "0".

You can use sprintf to do that.
The problem is that some field have "00" for the number of males
employed, thus I try to test whether all two positions are filled
before the assignment using the expression below, but it seemingly
doesn't work,
elsif (/EMPmale=(\S+)/) {
if ($1 == /\S{2}/) {$Emale = $1 ;}
else {$Emale = "0$1" ;}
}

First off, 'the number of employees' is a number not just a string of
non-space characters.

Second, it seems more intutive to me to use printf/sprintf for this kind
of task.

#! perl

use strict;
use warnings;

my $Emale;

while(<DATA>) {
if(/^EMPmale\s?=\s?(\d+)/) {
$Emale = $1;
} else {
$Emale = -1;
}

$Emale = $Emale < 0 ? 'NA' : sprintf('%2.2d', $Emale);
print $Emale, "\n";
}

__DATA__
EMPmale = 19
EMPmale= not known
EMPmale=3
EMPmale= 000005
EMPmale =99


C:\Home>t.pl
19
NA
03
05
99
 
T

Tad McClellan

Vumani Dlamini said:
if ($1 == /\S{2}/) {$Emale = $1 ;}


You should always enable warnings when developing Perl code.

Hope you girls (and
possibly guys) help me as always.


Hope you would have the courtesy to ask a machine to help
you *before* asking thousands of real people to spend
their time on it.

Enable warnings.
 
V

Vumani Dlamini

Thanks guys. I have never used "use warnings" before. I am what you
call a gatecrasher to Perl. A problem came up which I thought Perl
could do better than text processing facilities in statistical
software.

Thanks again guys, hope you don't get tired of me, like Ted.

Vumani
 
T

Tad McClellan

Thanks again guys, hope you don't get tired of me, like Ted.


To avoid becoming wearysome, simply see the Posting Guidelines
that are posted here frequently...
 
S

Schlick

Tad said:
To avoid becoming wearysome, simply see the Posting Guidelines
that are posted here frequently...

To become wearisome, simply quote them in every single one of your messages.
 
A

Anno Siegel

Schlick said:
To become wearisome, simply quote them in every single one of your messages.

We're not here to entertain you, we want a workable newsgroup.

Anno
 
B

Ben Morrow

Thanks again guys, hope you don't get tired of me, like Ted.

It's generally considered courteous to get people's names right,
especially when they're trying to help you.

Ben
 
G

Gunnar Hjalmarsson

Anno said:
We're not here to entertain you, we want a workable newsgroup.

True. Nevertheless, personally I believe that the newsgroup might be
even more workable if a _link_ to the posting guidelines was appended
to each message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top