blackhawk down has asked for the wisdom of the Perl Monks concerning the following question:

greetings all monks,

The Dilemna -> I need to create then compare two arrays extracting from them values from both sides when the partial entry from the first array is equated in the second

In Depth -> I have a file full of device names all containing unique entries in the style of loc-city-devicetype. I also have a second file that contains all the city names (can you see where I am going with this) I want to take the -city- part of the device name and compare it to the list of cities, then find a match and print the full device name and then the city name right after it.

example: dev-wic1-7700-g2-v1008 is a device in Wickliffe KY. the dev-wic1... is in the first file (along with about 3000 others) all one entry per line. The second file contains City and State names (also one entry perline) I want to equate wic1 to Wickliffe and then print out something like
- Wickliffe dev-wic1-7700-g2-v1008 and do so for every device in the first file.
Thanks for any enlightened insight you can give me

-blackhawk down

Replies are listed 'Best First'.
Re: Fun with arrays
by Abigail-II (Bishop) on Feb 12, 2004 at 20:39 UTC
    This is just a variation on one of the most asked questions, and therefore a FAQ.

    Study perldoc -q intersection, and come back if you have some code to show.

    Abigail

Re: Fun with arrays
by broquaint (Abbot) on Feb 12, 2004 at 20:25 UTC
    Hashes sound like the way forward
    open(my $cities_fh => 'cities.txt') or die "ack: $!"; my %cities; chomp, $cities{lc substr $_, 0, 3} = $_ while <$cities_fh>; open(my $devices_fh => 'devices.txt') or die "ack: $!"; /^dev-([a-z]+)/ and print "$cities{$1} $_" while <$devices_fh>;
    So create a hash with the keys as the first three letters of the city, lower-cased, and the values as the city & state name. Then iterate through the devices file grabbing the city identifier then printing out the city & state name along with the filename.
    HTH

    _________
    broquaint

Re: Fun with arrays
by arden (Curate) on Feb 12, 2004 at 20:24 UTC
    This smells very strongly of homework! Anyway, first it would be very nice if you were to try to do this on your own. We're all very happy to help you find errors in your code/logic and to suggest alternative ways of doing things, but not too many of us enjoy writing your code for you, especially if it is schoolwork.

    You might want to give us examples of the two files too. From what you've given us, I'd say telling us the first two lines of file 1 and the matching two lines from file 2 would work. I'm not sure if there are key elements that match up your arrays or if you just want to associate the first entry in file 1 with the first entry in file 2, etcetera. . .

    Either way, you need to open both files, read data in, process it through an algorithm (which at this point isn't clear), and put the data into an array (or maybe a hash). You haven't said how you plan to output the results either, to file or to screen. . .

    - - Arden.

Re: Fun with arrays
by artist (Parson) on Feb 12, 2004 at 20:34 UTC
    Do you have data in sequencial order ? ie.. wic1 => Wickliffe and wic2 => Wickville? If that's the case, it is very easy for you. From cityfile you can generate 'wic1' type entries, put it in the hash, read device file line-by-line and make outfile.
    If that's not the case, then you can apply String::Approx to do matching.

    artist.