Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

need help with concantenation

by Anonymous Monk
on Jan 12, 2003 at 06:03 UTC ( [id://226211]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
For the life of me I cannot concantenate these two variables.
"$month_3_tables" a Literal variable ..$x an Incremented variable inside of a for loop.
for($x=0;$x<$how_many_items;$x++){ if($item eq "March") {($month_3_tables.$x) .= "<tr> <td width='106' + height='24' valign='top'>$field_vals[0]</td> <td width='39' height=' +24' valign='top'>$field_vals[1]</td> <td width='590' height='24' vali +gn='top'>$field_vals[6]</td> </tr>"} }

Thanks

Replies are listed 'Best First'.
Re: need help with concantenation
by seattlejohn (Deacon) on Jan 12, 2003 at 06:26 UTC
    Without any surrounding context it's hard to determine what you're trying to accomplish here. Consider that $x .= $y is just shorthand for $x = $x . $y. So what you've written is this:
    $month_3_tables . $x = $month_3_tables . $x . "html stuff"
    which isn't meaningful, as you need to have a single variable name on the left side of the assignment statement.

    Is your intent to modify individual elements within an array, like this?

    $month_3_tables[$x] .= "html stuff"
    It might help if you followed up with a bigger snippet of code and/or a more complete explanation of what you expected the behavior to be.

            $perlmonks{seattlejohn} = 'John Clyman';

Re: need help with concantenation
by Coruscate (Sexton) on Jan 12, 2003 at 09:58 UTC

    The following snippet is what you want to use here (assuming I understand what you are trying to accomplish):

    my @month_3_tables; for my $x (0 .. $how_many_items) { if ($item eq "March") { push @month_3_tables, "<tr>.....</tr>"; } }

    Note that my example uses a programmer-friendly version of the for{} loop. Also, I'm not sure if you're using an if() statement for every month, but if not, then using push() to add an item to an array is much more efficient than trying to name variables such as $month_3_tables0, $month_3_tables1, $month_3_table2, etc, etc.

    ($month_3_tables.$x) .= "..."
    The above line that you have within your program is invalid perl, as has already been stated within another reply to your post. Perl just doesn't work that way for creating variables. Technically, you could use the following line to replace yours:

    ${$month_3_tables . $x} = "<tr>...</tr>";

    But that is just not the best way to go about this. Use an array, since this is the exact reason they were created :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://226211]
Approved by tadman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-24 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found