Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: searching array of arrays

by bikeNomad (Priest)
on Aug 02, 2001 at 10:16 UTC ( #101590=note: print w/replies, xml ) Need Help??


in reply to searching array of arrays

You don't show us what not's working. Just from glancing at the code you show, it looks like you will, in fact, build the data structure you describe (an array of references to arrays with strings in them). Note that the last fields in your arrays will have newlines; you don't say whether that's a problem (chomp is the usual way to deal with this).

You also don't say why you're building the whole data structure in memory before doing the search (are you?) rather than searching on the fly as the data comes in. But assuming that's what you have to do for some reason, you can just iterate through your array:

for my $a (@foo) { if ($a->[0] eq 'bill') { $a->[1] += 3; # add 3 to second field $a->[2] .= "!"; # add ! to third field } }

No reason to replace the array, as you've already replaced the values in it. That is, unless you have references to it elsewhere, in which case you could copy it:
for my $i (0 .. $#foo) { my $a = $foo[$i]; if ($a->[0] eq 'bill') { my $b = [ @$a ]; # copy a's data to b $b->[1] += 3; # add 3 to second field $b->[2] .= "!"; # add ! to third field $foo[$i] = $b; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2021-05-13 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (134 votes). Check out past polls.

    Notices?