Hi,

I would like to gather two types of information and put into an array. For instance, I have a file with 4 lines of info for each entry - I want to get the name and occupation only for each entry. How can I get just the name and occupation in an efficient manner?

File example:

Name : Bob Smith
address : somewhere, USA
gender : male
Occupation : Engineer

Output should be:
Bob Smith, Engineer

So far I can just gett the names - not both name and occupation.

while ($line1 = <LIST>)
{
chomp;
if ($line1 =~ /^Name\s+\:\s(\S+)\s+/) {
$name = $1;
push (@info, "$name\n");
}
}

Thanks for any help!

I just created this real fast on my local web server. In my example I looped through an array and its contents, pretty much doing the same thing that reading straight from a file ("while ($line1 = <LIST>)") will do. While looping through all the content, it catches the name and occupation. When it finds the occupation it changes the value of a variable ($NameOccPush = "PusToArr";) flagging the script to know it can push both strings into the array.

The limitation with doing it like this can be found if you run my example. The name "NoShow MoFo" will not be pushed into your final array since he has no occupation within his 4 lines of data. The only way to check for this and make sure you still push "NoShow MoFo" in your final array would be add in more comparisons. Saying If the name you have grabbed is different then the one you are currently looping through AND $NameOccPush still equals XX, then push it into the array without an occupation. Sorry I do not have time to build this in. I need to start my actual day job :)

Also, I removed extra white space from $line1 while it looped through the content before doing any comparisons. This allowed my If statement RegExp to be very flexible. When I used your example, it only grabs the first name (\w will only grab 1 word). Using $line1 =~ /^Name:(.*)/, it will grab ANYTHING that might be in there. Not Just a word.. Just a phrase.. Just one thing.

I hope this helps!

-Jeff Conklin-

#!perl


@FileContents = (
"Name : Bob Smith",
"address : somewhere, USA",
"gender : male",
"Occupation : Engineer ",
"Name : Bobby Jones",
"address : anywhere, USA",
"gender : male",
"Occupation : Programmer",
"Name : NoShow MoFo",
"address : anywhere, USA",
"gender : male",
"Name : George W",
"address : Washington DC, USA",
"gender : male",
"Occupation : President"
);


$NameOccPush = "XX";
foreach $line1 (@FileContents) {
chomp($line1);


# Gets rid extra spaces (if there are any) around the :
$line1 =~ s/\s*\:\s*/\:/g;


# Removes any trailing white space
$line1 =~ s/\s*$//;


if ($line1 =~ /^Name:(.*)/) {
$CathName = $1;
}
elsif ($line1 =~ /^Occupation:(.*)/) {
$CathOcc = $1;
$NameOccPush = "PusToArr";
}


if ($NameOccPush =~ /PusToArr/) {
push (@Grabbed, "$CathName, $CathOcc");
$NameOccPush = "XX";
}


}


print "Content-type: text/html\n\n";
foreach $Caught (@Grabbed) {
print qq~$Caught<br>~;
}

Hi Jeff,

This looks great - I forgot about the elsif statement which works very well here.

Thanks for your quick response and help!
Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.