Re: An elementary question

Expand Messages
• ... while( ){ chomp; # Split it up, (white)space seperated my @x = split / s/; # Loop through last 2 fields for(@x[-2,-1]) { # 19 = Divide by 100, 20 = Divide
Message 1 of 2 , Jan 31, 2002
--- In perl-beginner@y..., "slavik944" <slavik944@m...> wrote:
> Hi, I've got an embarrasingly simple question but I dont know how to
> go about finding a way to figure it out.
>
> I have for example a file that contains the following:
>
> 19 01 01 22 2232 6853
> 20 01 01 22 2525 7854
> 19 01 01 22 3656 5419
> 20 01 01 22 5743 6848
>
>
> The first column in the is a code which would mean the following:
> 19 = divide by 100 and show 2 decimal points
> 20 = divide by 10 and display decimal point
>
> The last 2 columns will be the values that should be divided based on
> the code in the first column.
>
> So if the first column of the first record has a value of 19 then
> the 5th column of the first record would be something like:
>
> 2232/100 = 22.32
>
> So that record would look like:
>
> 19 01 01 22 22.32 68.53
>
> And so on for the next records after that.
>
> So how do I go about doing this in PERL?
>
> I should know this but sometimes I get stuck by thinking in nawk or
> shell.
>
> Thank you,
> Steve M

while(<>){
chomp;

# Split it up, (white)space seperated
my @x = split /\s/;

# Loop through last 2 fields
for(@x[-2,-1]) {
# 19 = Divide by 100, 20 = Divide by 10
\$_ /= (\$x[0] == 19 ? 100 : 10);
}

# Put it all back together
print join(' ',@x), "\n";
}

# Or from command line:
perl -pe "chomp;@x=split/\s/;\$_/=10**(21-\$x[0])for(@x[-2,-1]);\$_=join(' ',@x).\$/;" < test.dat

- Brian
Your message has been successfully submitted and would be delivered to recipients shortly.