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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join / Map Help Please 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I'm tying to use the join and map to pipe delimiter a string from an array of hashes, I have the following but I just get an empty string?

Code:
 my $adv = join('|', map{$_->{'ID'}.','.$_->{'FirstName'}.' '.$_->{'LastName'}}@adv );

@adv has an array of hashes in it.

So i want $adv to end up with the following type value...
432,John Smith|567,Jane Doe|983,Paul Jones

What am I doing wrong?

Thanks,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
If you're experimenting to figure out how to use map and join, great! If you're putting it in production code, you might want to consider a temporary array with a foreach loop or something similar that's easier to understand when you come back to it six months later. So here's a way to do it both ways:

Code:
my @AofH = (	{	'id' => 432, 'first' => 'John', 'last' => 'Smith' },
				{	'id' => 567, 'first' => 'Jane', 'last' => 'Doe'},
				{	'id' => 983, 'first' => 'Paul', 'last' => 'Jones'}
			);
			
my @temp;
foreach (@AofH) {
	push @temp, "$_->{id},$_->{first} $_->{last}";
}
print join('|', @temp), "\n";
or, with the map and join
Code:
print join('|', map {"$_->{id},$_->{first} $_->{last}"} @AofH), "\n";
 
If you're experimenting to figure out how to use map and join, great! If you're putting it in production code, you might want to consider a temporary array with a foreach loop or something similar that's easier to understand when you come back to it six months later.

Why you say that Rharsh?

I can read your join statement perfectly well, it isn't I don't understand it , I just got the syntax wrong. I should have just double quote encapsulated the map part and allowed variable interpolation!

Got ya, many thanks!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top