XP is just a number PerlMonks

Re: Replacing values in an array

by eyepopslikeamosquito (Chancellor)
 on Jan 26, 2013 at 23:26 UTC ( #1015544=note: print w/replies, xml ) Need Help??

in reply to Replacing values in an array

When dealing with duplicates in Perl, you should normally use a hash (perldoc -q duplicate). My solution (written before seeing toolic's) is essentially the same as his, though I excluded the check for "M" since all your test data contains "M".

use strict;
use warnings;
use Data::Dumper;

my @array = ("M94202", "M94150", "M94297", "M94150", "M94161", "M94161
+", "M94162");

my %seen;
my $z = 1; foreach my$item (@array) {
if (exists $seen{$item}) {
$item =$seen{$item}; } else {$seen{$item} =$z;
$item =$z;
$z += 2; } } print Dumper \@array; print "\n"; [download] Replies are listed 'Best First'. Re^2: Replacing values in an array by tonto (Friar) on Jan 26, 2013 at 23:43 UTC Seeing the same solution written differently makes it more clear to me, I am very grateful to you. I will study hashes until I get them through my thick skull! I have spent days on this. Again, my sincere thanks! -tonto You can consider a hash to be much like an array, except instead of numerical indexes to access individual elements, you use strings as keys. # Define an array: my @basket = qw(apple banana cherry); # Get an element from the array: # (remember that indexes are 0-based) print "The second kind of fruit in the basket is$basket[1]\n";

# Change an element:
$basket[1] = "date"; print "Now it is$basket[1]\n";
[download]

The above example shouldn't be unfamiliar. Now, instead of keeping a @basket that tells us what kinds of fruit we have in the basket, let's keep a %basket that can also tell us how much of that kind of fruit we have.

# Define the hash:
apple    => 12,
banana   =>  6,
cherry  =>  32,   # This final comma is optional,
);                    # but makes it easier to add more lines in the f
+uture.

# Get an element from the basket:
print "There are $basket{cherry} cherries in the basket.\n"; # Modify elements:$basket{cherry}--;
print "Now there are $basket{cherry}.\n";$basket{banana} *= 2;
print "Double Banana Bonus! $basket{banana} bananas in the basket!\n";$basket{apple} = 10;

$basket{date} = 16; # Get all keys in the hash: print "Fruits in my basket: ", join(", ", sort keys %basket), "\n"; # Using a variable as a key: for my$fruit (sort keys %basket) {
print "You want a(n) $fruit? I have$basket{$fruit} in my basket.\ +n"; } # The 'each' function: while (my ($fruit, $amount) = each %basket) { print "There are$amount ${fruit}s in my basket.\n"; } # Getting rid of an element: delete$basket{apple};
print "Fruits in my basket: ", join(", ", sort keys %basket), "\n";
[download]

That pretty much covers the basics of hashes. Nothing to be afraid of, and quite a useful data type!

Create A New User
Node Status?
node history
Node Type: note [id://1015544]
help
Chatterbox?
 [Corion]: Do you want to launch a script and keep the command prompt/console window open? [Corion]: Do you want to wait for a key press before closing the window? [LanX]: I want the command line in the history [tye]: -Mouse [Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file. [LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy [Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history LanX damns the cult of CB ;-) LanX WTF WTF WTF [LanX]: please forget my last 3 posts

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2017-03-27 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (320 votes). Check out past polls.