Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Decimal Value of an element of an Array 1

Status
Not open for further replies.

hello99hello

Programmer
Jul 29, 2004
50
CA
Hi All,

All I want to do is change the decimal value of 3rd element in my array records.

Here is my inpu records:

al green 00.0000
jesse cal 02.00
laura Morgan 20.0

My expected output should look like the following:

al green 0.0000
jesse cal 2.0000
laura Morgan 20.0000

Here is my effort:

chomp $array[2] ;
$recordf = %3.4f, $array[2] ;
$array[14]= $recordf ;

However, this did not work, any help would be appreciated.
 
$array[2]=$array2*100;

HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
This did not work as it pultiplies the variable value by 100 and did not preserve the decimal four decimal places.

Again here is another input:

al green 0
jesse cal 02.5
laura Morgan 20
Julie Hines 275

My expected output should look like the following:

al green 0.0000
jesse cal 2.5000
laura Morgan 20.0000
Julie Hines 275.0000


Thanx in advance for your help, perharps I was not clearer in the first request.
 
Code:
#!perl
use strict;
use warnings;

my @array;
while (<DATA>) {
    chomp;
    my ($fname, $lname, $num) = /(\S+)\s+(\S+)\s+(\S+)/;
    push(@array, [ $fname, $lname, [b]sprintf("%.4f", $num)[/b] ]);
}
for my $row (@array) {
    print join(" ", @$row), "\n";
}

__DATA__
al green 0
jesse cal 02.5
laura Morgan 20
Julie Hines  275

[b]Output[/b]
al green 0.0000
jesse cal 2.5000
laura Morgan 20.0000
Julie Hines 275.0000

By the way, when something doesn't work, it would be helpful if you could give a more accurate description than "doesn't work," as you've done in several of your posts.
 
Thanx a lot guys last solution works......do you guys know of a good book on perl.
 
faq219-5394 in this forum

--Paul

--Sorry bout earlier, didn't look at the problem for long enough

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top