in reply to find and replace project with values coming from a table
I hope this helps your understanding, and also that it helps you understand some of the Perl idiom.#!/usr/bin/perl use strict; use warnings; use English; # Table 1 contains the find and replace values; my %substs = ( "jones" => "B0", "smith" => "B1", "adrew" => "B1", "larry" => "B3", ); # The 2nd table contains the data that I want to search in. my @data = ( [ "jones", "AAAAA", "BBBBB", "CCCCC"], [ "aaaaa", "AAAAA", "larry", "CCCCC"], [ "jones", "AAAAA", "BBBBBB","CCCCC"], [ "DDDDD", "AAAAA", "BBBBBB","larry"], [ "jones", "AAAAA", "andrew","CCCCC"], [ "jones", "smith", "BBBBBB","CCCCC"] ); # Loop over every element in the @data array. foreach my $row (@data) { foreach my $elem (@$row) { # $elem is an 'alias' for the value in the array, so # changing $elem changes the corresponding value in the # array. if ($substs{$elem}) { # We only want to change $elem when $elem is a key in the # substs hash. $elem = $substs{$elem} } } } # Print ", " between array values when the array is printed. $LIST_SEPARATOR = '", "'; # And show the resulting array. foreach my $row (@data) { print "[\"@$row\"]\n"; }
Arjen
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: find and replace project with values coming from a table
by demerphq (Chancellor) on Jul 05, 2003 at 13:54 UTC | |
by Abigail-II (Bishop) on Jul 05, 2003 at 22:20 UTC | |
by demerphq (Chancellor) on Jul 06, 2003 at 11:32 UTC | |
by Abigail-II (Bishop) on Jul 06, 2003 at 21:23 UTC | |
by demerphq (Chancellor) on Jul 06, 2003 at 22:24 UTC | |
| |
by Anonymous Monk on Jul 05, 2003 at 23:39 UTC |