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

join, why map required? - clarification appreciated. 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

Now I've actually worked out how to acheive what I want, I'm just curious why it has to be done this way, so clarification is appreciated.

I have an array which stores the months of the year, I wan't to generate a tabular report and so wanted to use join to build the table columns easily...

The solution i found was this...
Code:
$tab .= '<td class="titlecell">' . join('<td class="titlecell">',map{$_ . '</td>'}@mnames );

why do you have to map it and use join in this manner, why isn't it as simple as...

Code:
$tab .= join('<td class="titlecell">' . $_ . '</td>',@mnames);

To me the above seems more logical, but I guess I'm missing something as it doesn't work and I have to use the alternative I've shown.

I thought map was for generating hashes from arrays, so I'm a little confused why the string has to start the way it does and why map has to be used.

I simply want it to go through the array concatenating $_ with the string, only it doesn't seem to be that simple.

So I'd appreciated anyone's input as to what is actually going on.

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
 
To explain what map does, you should RTFM. :)

perldoc -f map said:
map BLOCK LIST
map EXPR,LIST
Evaluates the BLOCK or EXPR for each element of LIST (locally
setting $_ to each element) and returns the list value composed
of the results of each such evaluation. In scalar context,
returns the total number of elements so generated. Evaluates
BLOCK or EXPR in list context, so each element of LIST may
produce zero, one, or more elements in the returned value.

@chars = map(chr, @nums);
Here is a more straight forward (and hopefully easier to understand) way to use the map and join functions to get the results you want:
Code:
$tab .= join '', map {'<td class="titlecell">' . $_ . '</td>'} @mnames;
 
I usually accomplish these kinds of things like this:

Code:
print "<ul>\n";
print "<li>" . join("</li>\n<li>", @items) . "</li>\n";
print "</ul>\n";

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks guys.

It always makes sense when you kind people help out.

Rharsh
------
RTFM = Huh? What?
TT = Nice One!

Kirsle
------
I knew I should have been able to just use join, it was just a matter of getting the syntax right!

I wrongly thought (due to something else i'd done using map to create a hash), that $_ was populated with the next element in the array, which needed to be used in the concatenation.

Obviously join already adds the elements without needing to reference $_ for a standard join.

"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
 
You know, I was thinking - obviously you can use join, and maybe that's the right way to go, but I would probably use a for/foreach loop:
Code:
for (@mnames) {$tab .= '<td class="titlecell">' . $_ . '</td>'};
 
Yeah but, join is a one liner, I dont write my for loops as one liners ;-)

I actually introduce an inefficient use of the for loop later on in the code instead of using a hash to eliminate multiple itterations of an array, but i'm not looking for total efficiency

(Though I might alter the code to use a dynamic hash so only one itteration of the loop is required).

I guess I forced myself to use the 'join' method, because I'd been given it as a solution for a few other things I do and I so I set myself a test to finally & properly understand what the join did and how to use it.

I've found there really is no substitution to properly understanding how to do things and writing the code yourself, 'cargo cult' programming seems like a good idea at the time and saves time in the begining, but when things go wrong and you can't understand why as you didn't write the code, it can come back and bite you in the ass!



"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
 
rharsh said:
Read The Fine[sup]*[/sup] Manual.


[sup]*[/sup]for localisation and internationalisation purposes, this may be substituted with other words while still retaining the same acronym... [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Problem is there is a big difference between RTFM and UTFM...

Reading-TFM & Understanding-TFM!

"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