Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need help on AoH or array or any other think that might help!

Reply
Thread Tools

Need help on AoH or array or any other think that might help!

 
 
cyrusgreats@gmail.com
Guest
Posts: n/a
 
      10-17-2008
Hello good people out there. I need to write a perl script that will
parse the log and generate the following output:
a flat text file in the following format: customer_name|
product_category|item_description|cost
An example log file could be:
Pedro|groceries|apple|1.42
Nitin|tobacco|cigarettes|15.00
Susie|groceries|cereal|5.50
Susie|groceries|milk|4.75
Susie|tobacco|cigarettes|15.00
Susie|fuel|gasoline|44.90
Pedro|fuel|propane|9.60

1.A report of the total revenue resulting from each customer.
2.A report for each customer showing how much of their spending went
to each category.
With the above example log file, the script should output should be
something like:
Total Revenue:
Pedro - $11.02
Nitin - $15.00
Susie - $70.15

Purchases by Pedro:
groceries - $1.42
fuel - $9.60

Purchases by Nitin:
tobacco - $15.00

Purchases by Susie:
groceries - $10.25
fuel - $44.90
tobacco - $15.00


Any help will be appreciated...million thanks in advance
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      10-17-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello good people out there. I need to write a perl script that will
> parse the log and generate the following output:


What have you done so far and what questions do you have?

> Any help will be appreciated...million thanks in advance


Put in some sort of effort by at least reading some
documentation, or maybe a book, and posting your code.

perldoc -f split
perldoc -f open
perldoc perldsc
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-17-2008
(E-Mail Removed) wrote:
>Hello good people out there. I need to write a perl script that will
>parse the log and generate the following output:
>a flat text file in the following format: customer_name|
>product_category|item_description|cost
>An example log file could be:
>Pedro|groceries|apple|1.42
>Nitin|tobacco|cigarettes|15.00
>Susie|groceries|cereal|5.50
>Susie|groceries|milk|4.75
>Susie|tobacco|cigarettes|15.00
>Susie|fuel|gasoline|44.90
>Pedro|fuel|propane|9.60


perldoc -f split:
($customer, $category, undef, $price) = split ('|');

Then add the totals in hashes:
$byCategory{$customer}{$category} += $price;
$totals{$customer} += $price;

>1.A report of the total revenue resulting from each customer.
>2.A report for each customer showing how much of their spending went
>to each category.


Walking through the hashes and printing the accumulated data should be
trivial now.

jue
 
Reply With Quote
 
cyrusgreats@gmail.com
Guest
Posts: n/a
 
      10-18-2008
On Oct 17, 2:59*pm, "J. Gleixner" <(E-Mail Removed)>
wrote:
> (E-Mail Removed) wrote:
> > Hello good people out there. I need to write a perl script that will
> > parse the log and generate the following output:

>
> What have you done so far and what questions do you have?
>
> > Any help will be appreciated...million thanks in advance

>
> Put in some sort of effort by at least reading some
> documentation, or maybe a book, and posting your code.
>
> perldoc -f split
> perldoc -f open
> perldoc perldsc


here portion of the code:

my %inventory;
open (FILE, $file) || die ("Could not open file. $!");
foreach my $el (<FILE)>) {
($name, $category, $item, $price) = split ("|", $el);
my %s = (
'name'=>$name,
'product'=>$category,
'item'=>$item,
'price'=>$$price,
);
push @arr, \%s;
}

foreach $el (@$arr) {
$inventory{$el)->{'name'} = {name=>$el->{'name'}};
$inventory{$el)->{'product'} = {category=>$el->{'category'}};
$inventory{$el)->{'item'} = {item=>$el->{'item'}};
$inventory{$el)->{'price'} = {price=>$el->{'price'}};
}

Am I in right track?



 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-18-2008
(E-Mail Removed) wrote:

> On Oct 17, 2:59Â*pm, "J. Gleixner" <(E-Mail Removed)>
> wrote:
>> (E-Mail Removed) wrote:
>> > Hello good people out there. I need to write a perl script that
>> > will parse the log and generate the following output:

>>
>> What have you done so far and what questions do you have?
>>
>> > Any help will be appreciated...million thanks in advance

>>
>> Put in some sort of effort by at least reading some
>> documentation, or maybe a book, and posting your code.
>>
>> perldoc -f split
>> perldoc -f open
>> perldoc perldsc

>
> here portion of the code:
>
> my %inventory;
> open (FILE, $file) || die ("Could not open file. $!");
> foreach my $el (<FILE)>) {


^^^ You should always copy and paste your actual code you're using. The
above is broken: (<FILE)>)

> ($name, $category, $item, $price) = split ("|", $el);


I don't think that's going to do what you want. Did you print the
output to ensure it's going to work how you want, before you save the
values into a hash. I'd start there, fixing those issues, before you
try and move onto saving the hash keys/values and processing against
it.

--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-18-2008
Jürgen Exner wrote:
> (E-Mail Removed) wrote:
>> Hello good people out there. I need to write a perl script that will
>> parse the log and generate the following output:
>> a flat text file in the following format: customer_name|
>> product_category|item_description|cost
>> An example log file could be:
>> Pedro|groceries|apple|1.42
>> Nitin|tobacco|cigarettes|15.00
>> Susie|groceries|cereal|5.50
>> Susie|groceries|milk|4.75
>> Susie|tobacco|cigarettes|15.00
>> Susie|fuel|gasoline|44.90
>> Pedro|fuel|propane|9.60

>
> perldoc -f split:
> ($customer, $category, undef, $price) = split ('|');


The '|' character is special in a regular expresion. If you want to
match a literal '|' character you have to escape it:

my ( $customer, $category, undef, $price ) = split /\|/;

Or:

my ( $customer, $category, $price ) = ( split /\|/ )[0,1,3];



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-18-2008
"John W. Krahn" <(E-Mail Removed)> wrote:
>Jürgen Exner wrote:
>> perldoc -f split:
>> ($customer, $category, undef, $price) = split ('|');

>
>The '|' character is special in a regular expresion. If you want to
>match a literal '|' character you have to escape it:


Ooops, you are right.

jue
 
Reply With Quote
 
cyrusgreats@gmail.com
Guest
Posts: n/a
 
      10-20-2008
On Oct 18, 6:07*am, Jürgen Exner <(E-Mail Removed)> wrote:
> "John W. Krahn" <(E-Mail Removed)> wrote:
>
> >Jürgen Exner wrote:
> >> perldoc -f split:
> >> * * * *($customer, $category, undef, $price) = split ('|');

>
> >The '|' character is special in a regular expresion. *If you want to
> >match a literal '|' character you have to escape it:

>
> Ooops, you are right.
>
> jue


Thanks all I got it to work..
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      10-20-2008
Tim Greer wrote:
> (E-Mail Removed) wrote:
>
>> On Oct 17, 2:59 pm, "J. Gleixner" <(E-Mail Removed)>
>> wrote:
>>> (E-Mail Removed) wrote:
>>>> Hello good people out there. I need to write a perl script that
>>>> will parse the log and generate the following output:
>>> What have you done so far and what questions do you have?
>>>
>>>> Any help will be appreciated...million thanks in advance
>>> Put in some sort of effort by at least reading some
>>> documentation, or maybe a book, and posting your code.
>>>
>>> perldoc -f split
>>> perldoc -f open
>>> perldoc perldsc

>> here portion of the code:
>>
>> my %inventory;
>> open (FILE, $file) || die ("Could not open file. $!");
>> foreach my $el (<FILE)>) {

>
> ^^^ You should always copy and paste your actual code you're using. The
> above is broken: (<FILE)>)


Define 'broken'. It's not good code (OP use 'while' instead
of foreach), but it will run.

>
>> ($name, $category, $item, $price) = split ("|", $el);

>
> I don't think that's going to do what you want. Did you print the
> output to ensure it's going to work how you want, before you save the
> values into a hash. I'd start there, fixing those issues, before you
> try and move onto saving the hash keys/values and processing against
> it.
>

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-20-2008
"J. Gleixner" <(E-Mail Removed)> wrote:
>Tim Greer wrote:


>>> foreach my $el (<FILE)>) {

>>
>> ^^^ You should always copy and paste your actual code you're using. The
>> above is broken: (<FILE)>)

>
>Define 'broken'. It's not good code (OP use 'while' instead
>of foreach), but it will run.


It doesn't even parse (Tim pointed it out verbatim). I guess that
qualifies as broken by any sensible definition of broken.

jue
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Need help on AoH or array or any other help I can get for this task cyrusgreats@gmail.com Perl Misc 2 10-18-2008 01:11 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Need help: Is Quick-Union-Find the right solution to this problem (Now I don't think so and I think that topological sorting should be the way to go...?) ? aredo3604gif@yahoo.com C Programming 1 04-13-2005 12:48 AM
Need help: Is Quick-Union-Find the right solution to this problem (Now I don't think so and I think that topological sorting should be the way to go...?) ? aredo3604gif@yahoo.com C Programming 0 04-12-2005 05:06 PM



Advertisments