Gossamer Forum
Home : General : Perl Programming :

merge data ? omg

Quote Reply
merge data ? omg
well i have a data in text that is like this:
group user
group user2
group user3
group3 user
group3 user2
group4 user3

and i want to print it out.

but i want to print like this

group user, user2 and user3
group3 user and user2
group4 user3

any ideas ?

im getting headache trying to solve it.

like i dont know how to merge the group and still associating the user reading from a data base.

any1 give me an extra hand any doubt just ask .
thx

Last edited by:

NamedRisk: Mar 23, 2005, 6:59 AM
Quote Reply
Re: [NamedRisk] merge data ? omg In reply to
As long as your data set isn't huge you can read them it into a hash of array refs keyed on the group then print it:

Code:
#!/usr/bin/perl
use strict;
use warnings;

my %users;

foreach (<DATA>) {
chomp;
my ($g, $u) = split / /;
push @{$users{$g}}, $u;
}

print "Content-type: text/html\n\n";
print "<pre>\n";

foreach my $g (keys %users) {
print "$g " . join(', ', @{$users{$g}}) . "\n";
}

print "</pre>\n";


__DATA__
group user
group user2
group user3
group3 user
group3 user2
group4 user3

~Charlie

http://www.perldoc.com/...8.4/pod/perldsc.html

Last edited by:

Chaz: Mar 23, 2005, 8:11 AM
Quote Reply
Re: [Chaz] merge data ? omg In reply to
Quote:
As long as your data set isn't huge you can read them it into a hash of array refs keyed on the group then print it
How about of huge data? What synopse would you use?

I imagine, that the users for one group must be anyway collected, then printed.
Then print is done per group.

Code:
[find all unique groups in database]
[foreach groupname]
[get users for this group]
[print group & users]
[next foreach]
I was thinking about printing each user one by one, that would be need only for user number in a gruop would be really huge, so printing per group would be a good idea.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] merge data ? omg In reply to
In Reply To:
How about of huge data? What synopse would you use?

If it got to that point, I would switch to something like MySQL or PG to store the data instead of flat files. In fact, I'd opt for that to start with. I never did like working with flat files much :)

In Reply To:
I imagine, that the users for one group must be anyway collected, then printed.
Then print is done per group.

Then you'd potentially be looping over the file several times. Once to collect a list of groups then once for each group in the list to collect a list of users. That could get very slow if the data set was in fact large enough to require this. Again, thats an indicator that it's time to move to a RDBMS.

In Reply To:
I was thinking about printing each user one by one, that would be need only for user number in a gruop would be really huge, so printing per group would be a good idea.

I find it much easier to gather a list then print rather than printing as I find them. You have much more control over your data (i.e. sorting, formatting, etc.) in list form. If you just loop though the file and print them as you find them you lose much of that control. I guess it all depends on what you're doing with the data though.

One thing to remember is that a lot of this work can more efficiently be done at the DB level if you use MySQL or some other DB.

~Charlie
Quote Reply
Re: [Chaz] merge data ? omg In reply to
Quote:
If it got to that point, I would switch to something like MySQL or PG to store the data instead of flat files. In fact, I'd opt for that to start with. I never did like working with flat files much :)
Yes, I aggree, for huge data RDBMS is better, but that was not the original proposal... Smile
I don't like flat files, too Smile

Quote:
I find it much easier to gather a list then print rather than printing as I find them. You have much more control over your data (i.e. sorting, formatting, etc.) in list form.
Yes, but we supposed huge files, so loading everything into memory is not a possible option...
So for huge files, processing the less possible data together, then flush/print out is the only option.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [Chaz] merge data ? omg In reply to
Thx chaz u helped me alot with this ...

im not usual to hash in this way but i was read that page ... trying to understand it ...

i appreciate your help.

thx again..

and i agreed with u if it goings to be huge move to something better like mysql or pg