in reply to Re: Re: Re: comparing array values or?
in thread comparing array values or?

Well, we don't have those modules installed, otherwise I'd be happy to use them :). So let me ask this, how do I create an array of dynamically named arrays?

I'll assume:

@array = @array1,@array2,@array3, etc. Each array in @array is dynamically named.


Here's the code I've got to get started. It's not much, but hey...we got to start somewhere with it.
open(FILE, "data.txt"); #opens data.txt in read-mode while(<FILE>){ #reads line by line from FILE which i +s the filehandle for data.txt chomp; # print join(':', split(/,/,$_)); # print "\n"; @data = split(/,/,$_); } close FILE; #close the file.

So, am I being lame or what?

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: comparing array values or?
by danger (Priest) on Mar 14, 2001 at 07:38 UTC
    Well, we don't have those modules installed, otherwise I'd be happy to use them :). So let me ask this, how do I create an array of dynamically named arrays?

    Well, you almost certainly do *not* want dynamically named arrays. You more likely want either an array of array-references (probably anonymous arrays), or a hash of array-references. Or perhaps something altogether different -- we can't possibly know what you really want because you still have not adequately described what it is you are really trying to do. Use what fields in file1, and compare them to what in file2, and for what purpose? generate a report? sort one file according to a field in the other? merge into one file ordered by some field? delete data in file2 based on some user specified criteria that links to related data in file1?

    You may think you are presenting a simplified problem for us to help solve by omitting the larger picture and asking for specific implementation details, but you are incorrect in that assumption. Dynamically named variables are almost always the wrong answer, so rather than have us help you solve the wrong problem, you'd be better served to be more explicit in stating what you hope to achieve beginning with describing the data files, any other inputs the program is to receive, what the program is supposed to do with all of its input data and files, and following through with a description of the output you would like to arrive at. Do no hesitate to state the why's as well as the what's.

      Whew! You guys are tough. :) Okay...I understand what it is you're asking from me. I'll attempt to explain further and please be nice if I still don't get it right. :(

      I'll describe File 1 first.

      File 1 has three fields, customer id, customer name, and sender id.

      File 2 has 10 fields. The 2nd field is the sender id and the 5th field is organization type.

      My goal is to generate a report printing the entire list of customer names from file 1. For each customer in File 1 I need to know how many organizations in File 2 match the sender id from File 1.

      I'll also need a list of organization types from File 2 that do not match up with sender id from File 1 based on sender id.

      Does this help? I'd be more than happy to answer any further questions to help explain the goal or concept I'm trying to achieve.

      Thanks!

        Here's one possible approach.

        Read (the shorter) File 1 into memory. You could add each line to a hash, keyed on the sender ID.

        After that's done, loop through each line of File 2. You can probably use split or substr or even unpack to get at the individual fields. Check to see if there's a hash member with the sender id (with exists, so you don't get bitten by autovivification). If so, print the customer name to the match file. Otherwise, print the organization type to a second file.

        You don't have to print to files. You could push elements onto an array or assign to another hash, to avoid duplicates, if that's your trick. Using files is simple, if you open two filehandles before looping over the second file, and you can run them through uniq or something similar without using too much memory.

        Make sense?