If I understand you correctly, you are trying to process an array of an
array--each element of an array contains an anonymous array. Is this
correct?
Given the fact that you would like to pull out a specific user, you should
use a hash of a hash (a hash containing anonymous hashes).
Also, since you want to pull out particular info:
--user=1 --item=firstName
but user value of "1" is not contained in the data you posted. I am assuming
you mean the firstName of the first array value.
Lastly, you write that the data are "in a list format in an array" but you
include colons separate each array element which leads me to believe that
the data is really in plain text separated by colons.
I making many assumptions but here goes:
---------------BEGIN CODE---------------
#!D:\perl\bin\perl -w
use strict;
my ($id, $status, $user, $phone, $first, $last, $email) = 0 .. 6;
my @data;
my %data;
while (<DATA>) {
# You can do a pattern match:
# I did not parenthesize the field separator : below because
# I did not want it as an element in @temp.
# my @temp = $_ =~
# m{ ^(\d+) # id
# \( (\w+) \) : \s* # status
# (\w+) \s* : \s* # username
# (\d+) \s* : \s* # phone
# (\w+) \s* : \s* # first name
# (\w+) \s* : \s* # last name
# (.+)$ # email address
# }x;
# Or you can split $_
my @temp = split /[\(\):\s]+/;
# This is @data
$data[ $#data+1 ] = [ @temp ];
}
# Now you can directly print the email address of the 3rd record:
print "email: $data[2][$email]\n";
# But is that really helpful?
# You might want to build a hash with the username as keys:
foreach ( @data ) {
# This is %data
$data{ $$_[$user] } = [ @$_[0..$user-1, $user+1..$#$_ ] ];
}
print "id: $data{idiot}[$id]\n\n";
# You now have a hash with arrays as the values.
# You can NOT safely use the global variables
# $phone, $first, $last, $email
# from above because their values should have shifted down
# by one when I removed username; therefore....
foreach my $key ( keys %data ) {
my @values = @{$data{$key}};
# Necessary because you are replacing an array with a hash.
# If you do not undef, a "can't coerce" error occurs.
undef $data{$key};
# This is a hash slice.
# Refer to Perl Cookbook 4.7 p. 105
@{ $data{$key} }{ qw(id status phone first last email) } = @values;
}
# Print what's in the hash.
foreach my $id ( sort keys %data ) {
print "$id\n";
foreach my $field ( sort keys %{$data{$id}} ) {
print "\t$field\t$data{$id}{$field}\n";
}
}
# Or you can print oaf's last name:
print "\nlast: $data{oaf}{last}\n";
__DATA__
2233440000(IDLE): idiot : 1234567 : Iyam : Stupid : [EMAIL PROTECTED]
5566770000(IDLE): moron : 8901234 : Moe : Ron : [EMAIL PROTECTED]
8899000000(IDLE): oaf : 5678901 : Big : Giant : [EMAIL PROTECTED]
----------------END CODE----------------
-----Original Message-----
From: David Mamanakis [mailto:efialtis@;efialtis.com]
Sent: Friday, October 18, 2002 4:37 PM
To: [EMAIL PROTECTED]
Subject: another reg-ex question
these are common it seems...
I have a routine that returns a listing of users and all their information
in a list format in an array
1: <userIDnumeric>(status): <userName> : <phoneNumber> : firstName :
lastName : [email protected]
example:
1: 2233440000(IDLE): idiot : 1234567 : Iyam : Stupid : [EMAIL PROTECTED]
This list can be long.
The problem I am having is I need to know how to grab individual components
out of the listing...
Each list is an element in the array.
I can grab each list doing a "foreach" but I need a reg-ex to grab only the
username or only the phone number...
Earlier reg-ex's I have written I have split lists on commons like the ":"
but can I split this list into 7 parts then re-split # 2 again to have a
total of all 8 list parts?
This is not exactly what I am looking for...I would much rather like to
"plug in" what I am looking for and be able to grab it out..
i.e. I would like to input which user and which item I want and have it
output only those items...
--user=1 --item=firstName
User1-firstName=Iyam
I just need the reg-ex...I can work out the rest of the programming...
Thanks for any help...
--Dave Mamanakis
--Monroe, WA
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]