OK, so now you know that you can do what you want and tons of people have been thoughtful enough to tell you that you don't want to do that, but no one bothered to tell you why.

Generally, whenever you see variables whose names exhibit a consistent pattern such -- of which your $f1, $f2, $f3, etc. certainly qualifies -- it implies that these variables are logically grouped together. In your case, people suggested an array named @f. Another example would be variables like $customer_fname, $customer_lname, $customer_phone, etc. In this case, a hash would be a better choice:

%customer = ( fname => 'John', lname => 'Doe', phone => undef, );

If the variables really don't belong together, there's a good chance that they have been poorly named and are likely to mislead programmers looking at your code (I wonder how many lone programmers get irritated when they hear people talk about others looking at their code?).

However, if those variables are logically grouped together, they should probably be grouped together in your code in a single construct, such as an array or hash. There are a variety of reasons for this, but two of the biggest are clean code and ease of use. For example, I routinely see stuff like this:

sub thing { my $string = shift; my ($f0, $f1, $f2, $f3, $f4, $f5) = split /|/, $string; $f2 = $f4 + 5; return ($f0, $f2, $f3); }

Well, that's ugly. You're creating variables only to throw them away and it's not clear what you're doing. However, not only is this not clean code, it's also harder to work with. Let's say your boss wants to know why you're throwing away that second variable? So you add it:

sub thing { my $string = shift; my ($f0, $f1, $f2, $f3, $f4, $f5) = split /|/, $string; $f2 = $f4 + 5; return ($f0, $f1, $f2, $f3); }

Now you have a problem. Everywhere where you call this code will now have the $f2, and $f3 variables shifted away by one and you'll have to go through and fix all of them. Had you put the variables in an array and returned that, you may return extra data you don't need now, but the code is clean and easier to use later.

sub thing { my $string = shift; my @f = split /|/, $string; $f[2] = $f[4] + 5; return @f; }

There are still problems with that code (hard-coded constant and poorly named variables), but what happens if it turns out there is a $f6 and $f7 added later on? You may have to update some code to process it, but you might not have to update the sub to handle it because logically related variables were grouped together appropriately.

I hope I've shed a little light on that subject :)

Cheers,
Ovid

New address of my CGI Course.


In reply to Re: How to split a string (an explanation of why you wanted an array) by Ovid
in thread How to split a string by Red_Dragon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.