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:
How to search a pattern and add

 



Tejas
User

Oct 30, 2013, 1:02 AM

Post #1 of 6 (1272 views)
How to search a pattern and add Can't Post

Hi

I have 3 colums in a file
ID1, ID2 and Amount
I need to check how many times ID2 exists and add the amount
each time if it exists.

ID1 ID2 AMOUNT
6438742805,705058269513,.02
517314047,705519006516,12.99
6915433805,706390144520,-10.66
6915433805,706391479520,-20,05
6915433805,706390066520,-10.66
7074286205,706045811516,-14.9
997613316,705998908515,-20.5
451447246,706701645520,-23.74
891993586,705994405515,-14.95
1158141961,706152794518,-4.99
7076263205,706316988512,-18.99



Thanks
Tejas


BillKSmith
Veteran

Oct 30, 2013, 4:08 AM

Post #2 of 6 (1267 views)
Re: [Tejas] How to search a pattern and add [In reply to] Can't Post

I am not sure what you mean by 'exists'. I assume that you mean the field contains anything except whitespace.

What have you tried? What is your problem? We are here to help, not do your work for you.
Good Luck,
Bill


Tejas
User

Oct 30, 2013, 4:23 AM

Post #3 of 6 (1263 views)
Re: [BillKSmith] How to search a pattern and add [In reply to] Can't Post

ok
How many times that ID2(string/number) is repeated in the file
if the string is found ,then we have to add the amount to old amont ( i will take care of this addition)
This is what i hva etried .but in vain

my @identity = qw/'721249523520'/;


my $data_file = '/home/tejaswi/BR_MX/BR_MX_AU_UNBAL.dat';


open (DAT, $data_file) or die "$!";
while (<DAT>) {
if (/^NAME $identity$/) {
print;
while(<DAT>) {
print;
next if (m|//|);
}
}
}

close (DAT);

In Reply To


BillKSmith
Veteran

Oct 30, 2013, 5:36 AM

Post #4 of 6 (1256 views)
Re: [Tejas] How to search a pattern and add [In reply to] Can't Post

You have several errors. The largest by far is your nested while's. You should only read the file once.

Always begin a script with

Code
use strict; 
use warnings;

They do require you to declare your variables. You are already doing this.


'identity' contains a single value. It is a scalar and should be declared with a $ instead of @.

qw is for arrays. Just use the quotes. (Even these are not needed for a number)

Use a lexical file handle. (Start with a $. Declare with 'my')

Use the three argument form of open. (refer perldoc -f open)

Your data format is called 'csv' (comma separated values). Your regex is not even close to parsing correctly. In general I would recommend using a csv module. In this case, it would be easier to use the perl function split (refer perldoc -f split) to divide each line into fields.

Do the addition inside your main loop. (Declare the sum outside the loop.)

If you still need help, show us your revised code and ask specific questions.
Good Luck,
Bill


zapzap
User

Nov 3, 2013, 1:53 AM

Post #5 of 6 (1233 views)
Re: [Tejas] How to search a pattern and add [In reply to] Can't Post

Greetings,

I took a stab at your task. It's up to you to modify. I noticed that some of your data is incorrectly formatted so I didn't use yours. (comma instead of period on line 4)


Code
my %hash; 
map { /,(.+?),(.+?)$/;
$hash{$1} += $2 } <DATA>;

## You print results ##
## They're in %hash ##

__DATA__
abc,xyx,34.33
aac,xyy,33.02
aba,xzz,21.01
aaa,xyx,333.87
afa,xyy,12.10
aea,xzz,55.40


If somebody has a different approach or would like to comment upon mine for improvement, please do.


Laurent_R
Veteran / Moderator

Nov 3, 2013, 10:43 AM

Post #6 of 6 (1224 views)
Re: [zapzap] How to search a pattern and add [In reply to] Can't Post

It probably works, but I certainly would not do it that way. To me, this is a misuse of the map function (although you are obviously free to use it the way you want). And, especially, this is emphatically not what I would suggest to the OP, who is obviously a beginner.

An easier approach for a beginner might be something like this (type directly on this post and untested):


Code
my %summary; 
while (my $line = <DATA>) {
chomp $line;
my (undef, $id, $value) = split /,/, $line;
$summary{$id} += $value;
}
# print the summary here


 
 


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

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