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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

urgent question ( hash)???

Status
Not open for further replies.

guruu

IS-IT--Management
Nov 12, 2008
4
0
0
CA
i have a file with this format

Name ID Grade : those are seperated by tab

but in the array i have some entries ( name && ID duplicate)
i want a way to put those duplicate in one line and add the grades of each.

any idea?? maybe using hash ll help?? how can i put this array in a hash ??/

thx in advance

 
As we always ask.....

What have you tried?

Please note, student posting is not allowed here.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Also some examples of actual data/example data may help and the expected output. For example, do all duplicate names always have the same ID e.g.:

John 00001 grade A
John 00001 grade B

or:

John 00001 grade A
John 00002 grade B

if the latter, then how should should the ID's be processed in the same way as the grades should?

Chris
 
thank you for ur reply,

example :

John 00001 20
John 00001 30
Albert 00002 10
John 00003 10
Albert 00002 20
Albert 00004 20

result:
John 00001 50
Albert 00002 30
John 00003 10
Albert 00004 20

thx
 
yeah , actually , it s a very small part of a big program.

ok let me ask you if can tell me how i can put those values in a hash.

i tried this . it didn t work:

my %hash;
$hash{ $key } = $value; ( $key = Name ; $value = ID )

then my @temp = %hash;
print @temp;

i was trying for only 2 entries but also it didn t work


i want it for 3 entries.

thx
 
Are you a student?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
One possibility:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%hash[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [black][b]my[/b][/black] [blue]$IN[/blue], [red]'[/red][purple]path/to/file[/purple][red]'[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<[blue]$IN[/blue]>[red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
   [black][b]my[/b][/black] [red]([/red][blue]$n[/blue], [blue]$i[/blue], [blue]$s[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][red])[/red][red];[/red]
   [blue]$hash[/blue][red]{[/red][blue]$n[/blue][red]}[/red][red]{[/red][blue]$i[/blue][red]}[/red]+=[blue]$s[/blue][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] [blue]$IN[/blue][red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$name[/blue] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%hash[/blue][red])[/red] [red]{[/red]
   [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$id[/blue] [red]([/red][black][b]sort[/b][/black] [red]{[/red][blue]$a[/blue] <=> [blue]$b[/blue][red]}[/red] [black][b]keys[/b][/black] [blue]%[/blue][red]{[/red][blue]$hash[/blue][red]{[/red][blue]$name[/blue][red]}[/red][red]}[/red][red])[/red] [red]{[/red]
	   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$name[/blue] [blue]$id[/blue] [blue]$hash[/blue]{[blue]$name[/blue]}{[blue]$id[/blue]}[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [red]}[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top