Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Hash Question

by state-o-dis-array (Hermit)
on Jan 10, 2011 at 22:23 UTC ( #881553=note: print w/ replies, xml ) Need Help??


in reply to Hash Question

I'm going to go strictly off your description, as what you said you want to print out doesn't seem to match the description as I understand it.

I would just do something like this:

@ACCTS = ("1234", "5678", "6959"); open(IN,"InFile.txt"); %acct=""; while (defined($line = (<IN>))){ foreach $x (@ACCTS) { if ($line =~/$x/){ #only print the first line we see for each account unless( exists $acct{$x} ){ print "$line\n"; $acct{$x}++; } } } }
Or if you need to store all the data for other purposes:
@ACCTS = ("1234", "5678", "6959"); open(IN,"InFile.txt"); %acct=""; while (defined($line = (<IN>))){ foreach $x (@ACCTS) { if ($line =~/$x/){ push(@{$acct{$x}},$line); } } } foreach $key (sort keys %acct){ print "$key,$acct{$key}[0]\n"; }


Comment on Re: Hash Question
Select or Download Code
Re^2: Hash Question
by AnomalousMonk (Abbot) on Jan 10, 2011 at 23:18 UTC
    %acct="";

    Initializing a hash (and a non-lexical one for good measure) with a key of an empty string with the default value of undef produces a warning if warnings are enabled, as they almost always should be – along with strictures.

    use warnings; use strict;
Re^2: Hash Question
by mmittiga17 (Scribe) on Jan 11, 2011 at 19:33 UTC

    Thank you all very much for the help. With your help I learned something new and got my script to work. <\p>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://881553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-10-30 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (207 votes), past polls