CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Reading in a file and the manipulating it

 



WillST
New User

May 4, 2011, 6:18 PM

Post #1 of 7 (708 views)
Reading in a file and the manipulating it Can't Post

Hi,

I would like a simple and easy understand way to do the following.

I have a file “list” which looks like:
#D Door
#W Wheel
#E Engine
D 201.65
D 210.11
W 55.23
E 500
W 57.23

What I want to do is produce a report that looks like:
Door: 411.76
Engine: 500
Wheel: 112.46
Total: 1024.22

I am trying to get a hash that contains the full name and cost but as you can see the way the “list” file is laid out I have to somehow match the two but I am not sure how to do it. I am looking at pattern matching and I think this will probably be the way forward but am not sure how to start really. Though I would prefer a way that does not involve it as I believe it will make my program simpler.

Thanks for your help/suggestions!


rovf
Veteran

May 5, 2011, 1:35 AM

Post #2 of 7 (699 views)
Re: [WillST] Reading in a file and the manipulating it [In reply to] Can't Post

Since you are a beginner, maybe the simplest to understand would be to use two hashes: One for the abbreviations (i.e. mapping 'D' to 'Door'), and one for the running sum (i.e. mapping 'D' to the sum of numbers associated with key 'D').

Another possibility would be to use only one hash, where the key ('D') maps to a data structure holding the expansion AND the current sum. Depending on your preferences, this data sturcture could be a reference to an array or a structure, for example: 'D' would be mapped to ['Door', 411.76]. Of course this means that you already know about references in Perl (which you will need sooner or later anyway).


WillST
New User

May 5, 2011, 2:37 AM

Post #3 of 7 (696 views)
Re: [rovf] Reading in a file and the manipulating it [In reply to] Can't Post

Thanks so much, the reference approch sounds good to me.

As for having two hashes (this may be a silly question!) but how would I print the value (Door) from hash one and the value (411.76) from hash thus making the report?

Thanks again.


rovf
Veteran

May 5, 2011, 3:03 AM

Post #4 of 7 (694 views)
Re: [WillST] Reading in a file and the manipulating it [In reply to] Can't Post

Well, they share a common key!

Note that you can get all the keys from a hash using the "keys" function. See

perloc -f keys


WillST
New User

May 5, 2011, 3:58 PM

Post #5 of 7 (680 views)
Re: [rovf] Reading in a file and the manipulating it [In reply to] Can't Post

my $expensises_r = {D => [Drinks, 17.35],
F => [Food, 3.45],
G => [Gadgets, 50],
K => [Kids, 9.25],
M => [Mortgage, 320]};

for (keys %{$expensises_r})
{
print "value: ${$expensises_r}{$_} \n";
}

I have the above code and I am attempting to get the hash value to print however I am getting the memory address rather than the value itself. I think I am meant to also use something like @{$name_r} but not sure.

Any help would be appreciated.


miller
User

May 5, 2011, 5:48 PM

Post #6 of 7 (675 views)
Re: [WillST] Reading in a file and the manipulating it [In reply to] Can't Post

Just use separate hashes for the labels and totals.


Code
use strict; 
use warnings;

my %label;
my %total;

while (<DATA>) {
chomp;
if (/^#(\w+)\s+(\w+)/) {
$label{$1} = $2;
} else {
my ($key, $val) = split;
$total{$key} += $val;
}
}

for my $key (sort keys %label) {
print "$label{$key}: $total{$key}\n";
}

__DATA__
#D Door
#W Wheel
#E Engine
D 201.65
D 210.11
W 55.23
E 500
W 57.23


I leave the getting of the final total as an exercise for yourself.


rovf
Veteran

May 6, 2011, 3:41 AM

Post #7 of 7 (670 views)
Re: [WillST] Reading in a file and the manipulating it [In reply to] Can't Post

If $name is a reference to a 2-element-array: $name->[0] for the first element and $name->[1] for the second element of the array.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives