Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Few basic questions about arrays in PERL 2

Status
Not open for further replies.

blues77

Programmer
Joined
Jun 11, 2002
Messages
230
Location
CA
Hi,

I have just a few questions about arrays. First is there a command that will allow me to check to see if an array contains a particular value? Number two. Is there a way to erase all the contents of an array? I guess it would be good to know how to determine the length of the array so I could iterate through it. I'm new to PERL. Can't you tell.

Thanks!
 
Yes! ;-)
1) Searching an array is usually done with "grep" or looping through all elements and testing for some kind of match.
2) @array = ();
3) foreach (@array) { process_element_here($_); }

Does that help at all?


Trojan.
 
Sure does thanks. One more question about that regular expression we were working on before. I need to modify it so that if it is being compared to a string like
INDV_CLT_DECL_MRTL_SCD CAAS T1 Master Tape.IDENTIFICATION.MARITAL_STATUS_CD

It will extract one string after the first period. So in this example it would extract IDENTIFICATION.MARITAL_STATUS_CD. Here is what I have but it's only printing the value MARITAL_STATUS_CD. Here is my code.
Code:
while(<MY_FILE2>)
	{	
		chomp;
		my $line2 = $_;

		if($line2 =~ /^\s*$/)
		{
			#do nothing
		}
		elsif($line2 =~ /^($line1)\s+.+/)
		{
			#print "We found $line1 in the second file\n";
			if($line2 =~ /^($line1)\s+CAAS.+\.([^.])/ )
			{
				
				print "Inside";
				print ("Found the string $line1 in the file $ARGV[1]\n");			
				print ("Segment = $2 and COBOL Field Name = $2\n");
			}
		}
		
	}

Any idea why this isn't picking up the whole string IDENTIFICATION.MARITAL_STATUS_CD.

Thanks again for your help!
 
I made on change to the above code I now have

Code:
	if($line2 =~ /^($line1)\s+CAAS.+\.([^.]+)/ )
			{
				
				print "Inside";
				print ("Found the string $line1 in the file $ARGV[1]\n");			
				print ("Segment = $2 and COBOL Field Name = $2\n");
			}

But this still is only printing out MARITAL_STATUS_CD instead of IDENTIFICATION.MARITAL_STATUS_CD.
 
no regexp needed for substring extraction:

Code:
my $str = 'INDV_CLT_DECL_MRTL_SCD                            CAAS T1 Master Tape.IDENTIFICATION.MARITAL_STATUS_CD';
my $new_str = substr($str,index($str,'.'));
print $new_str;
 
Humm how exactly does that work?
 
index($str,'.') finds the location in the string of the first dot (.) in the string ($str)

substr() uses that value to pull out all the text after that position in the string and assign it to the scalar on the left side ($new_str).

You can look up all perl functions on this website:


Trojan will come back with a regexp solution if thats what you prefer. My preference is to use substr() and index() when working with sub-strings, which to me looks like what you are doing.
 
Nevermind I figured it out. Instead of doing a for each loop is there a command to check to see if an element exists in an array?
 
Basically I would like something like this

if ($1 exists in array1 and $2 exists in array2)
{
do something
}
 
No there is not (unless you count grep which actually returns an array of matches).
The best way is to create a HASH with your array items as the keys to the hash.
Then when you want to see if something exists, you truly can say "if exists $hash{$value}".


Trojan.
 
The hash would be the way to go, depending on the volume of data

TWB, how's the healing?

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Ok so say I write

my %my_hash =();
and then assign

$my_hash{$1} = $2;

Then am I only able to check to see if the key $1 exists or can i check to see that the value stored in $2 has been assigned to the $1 key. And further to that what's a quick way to iterate through all the values in the hash.
 
You can check the key AND the value as you suggest.
Quick way to iterate is "for (keys %hash)" for small data volumes or "while(($key, $value) = each %hash)" for large data volumes.

Paul, thumb is not too clever. Nerves all shredded so no feeling in the skin but frayed nerve endings means that any pressure it like an electric shock! Not nice. :-(

I guess I just have to learn to live with it. As they say, time is a great healer.



Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top