Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

concatening strings

by bory (Beadle)
on Nov 07, 2003 at 16:38 UTC ( [id://305358]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Wise Monks! I'm reading some data from a textfile and I want to put it in a html table cell, on the same row with other information which is the result of an html form. My text file looks like this:
Subject checkbox yes Data checkbox yes ....etc
I do this
my $d=scalar(@b)/3 # @b is the array with the content of the file for (my$i=0; $i<=$#ddts; $i++){ #@ddts is the array in which is the result of an html form for (my $index=0;$index<=($d-1);$index++) {my $x=(($#b-1)-$index*3);# i calculate the position of the second wor +d from each row, there is the html type i need $new="<td><input type=\"$b[$x]\" name=\"$b[$x-1]\"></td>"; print "<table><tr><td>$ddts[$i]</td>$new</tr></table>"; } }
I know that is not a very good way to create an html table but I don't understant way it's printing every row twice, when i want to print only once. If i have 2 checkboxes i want them on the same row not each on different row. I don't know how to concat the $new string with every element of the array @ddts. Thank you.

Replies are listed 'Best First'.
Re: concatening strings
by hardburn (Abbot) on Nov 07, 2003 at 17:05 UTC

    Hrm, you could really work on your indentation, as it would help you and others to read your code. Right now, your code is basically unreadable. Try applying a few rules like these:

    • Each indent is a tab, with your editor set to show 8 spaces for a tab (or 4 spaces, on languages with longer identifiers, like Java)
    • Curlies for conditions, loops, or labeled blocks go on the same line as the statement it goes with.
    • Curlies for subroutines are on a line by themselves.
    • After a curly, each line is indented one more than the indentation of the line the curly is on until the closing curly.

    You'll hear as many opinions on style as there are programmers. In particular, the first rule on the above list (about the definition of a "tab") is a big flame war issue. However, I know for sure that having some consistant style is infinatly better then no style at all.

    Note that the above isn't my complete style rules, but is enough to get your code in a much more readable state.

    Here's what I get when I apply the above rules to your code, getting rid of comments as I go:

    my $d=scalar(@b)/3; for (my $i=0; $i<=$#ddts; $i++) { for (my $index=0;$index<=($d-1);$index++) { my $x=(($#b-1)-$index*3); $new="<td><input type=\"$b[$x]\" name=\"$b[$x-1]\"></t +d>"; print "<table><tr><td>$ddts[$i]</td>$new</tr></table>" +; } }

    Which is much nicer to read, though it could still use some improvment (like maybe breaking up a few of the print statements, or using qq// instead of regular double quotes). You may find that you don't need to use so many comments anymore, since your code will be nice enough to read that you don't need them very often.

    Also note that when I was formatting the above, I found you had a missing semicolon that I bet would have been otherwise buried.

    To answer your actual question, you use the '.' operator to concatnate strings. The fastest and easiest way to concatnate an array is to use join.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      Yes, Perl::Tidy :)
      bash$ cat unreadable my $d=scalar(@b)/3 # @b is the array with the content of the file for (my$i=0; $i<=$#ddts; $i++){ #@ddts is the array in which is the result of an html form for (my $index=0;$index<=($d-1);$index++) {my $x=(($#b-1)-$index*3);# i calculate the position of the second wor +d from each row, there is the html type i need $new="<td><input type=\"$b[$x]\" name=\"$b[$x-1]\"></td>"; print "<table><tr><td>$ddts[$i]</td>$new</tr></table>"; } } bash$ perltidy unreadable bash$ cat unreadable.tdy my $d = scalar(@b) / 3 # @b is the array with the content of the fi +le for ( my $i = 0 ; $i <= $#ddts ; $i++ ) { #@ddts is the array in which is the result of an html form for ( my $index = 0 ; $index <= ( $d - 1 ) ; $index++ ) { my $x = ( ( $#b - 1 ) - $index * 3 ) ; # i calculate the position of the second word from each ro +w, there is the html type i need $new = "<td><input type=\"$b[$x]\" name=\"$b[$x-1]\"></td>"; print "<table><tr><td>$ddts[$i]</td>$new</tr></table>"; } } bash$
Re: concatening strings
by asarih (Hermit) on Nov 07, 2003 at 17:01 UTC
    Ouch. In Perl, there is often a better way to do an equivalent of the for loop that you describe. In particular, you should leave it up to perl to keep track of the indices. If your data is in input.txt, then
    open INPUT, "input.txt" or die "Can't open input.txt: $!\n"; print '<table>'; while (<INPUT>) { @words=split /\s+/; printf "<tr><td>%s</td><td><input type=\"%s\" name=\"%s\"></td></t +r>", $words[0], $words[1], $words[0]; } print '</table>'; close INPUT;
    would build a table for each line from the input. It's up to you to tweak the HTML.

    Update: Put <tr></tr> in the right places.

    Update 2: I tweaked HTML so that each checkbox gets more unique (but potentially not) name. :)

Re: concatening strings
by ChrisR (Hermit) on Nov 07, 2003 at 16:50 UTC
    Creating $new to hold every element of @ddts is as easy as my $new = (join '', @ddts);

    As for the rest of your post, I'm not sure I understand exactly what you are looking for. Please give us an example of the desired output.
      I try also with join but I don't understand why, if I have 2 elements in $new , the print line prints twice: one time the element o f the array @ddts + the first element of $new, and second time the same element of the array with the second element of $new. I belive that is because of the second for loop, exactly the value of $d. I want on the same line to have the element of the @ddts array and the elements of $new. Thank you very much
        Can you show me what your current output is and how you want it to look?
        A reply falls below the community's threshold of quality. You may see it by logging in.
(shockme) Re: concatening strings
by shockme (Chaplain) on Nov 07, 2003 at 17:19 UTC
    I'm not sure exactly what you're wanting, and your code is somewhat unformatted and difficult to follow. There are probably better ways, but the following should get you started:
    while(<DATA>) { my (@elements) = split(/\s/, $_); my $html = "<td><input type=\"$elements[1]\" name=\"$elements[0]\" +></td>"; print "<table><tr><td>$html</td></tr></table>\n"; } __DATA__ Subject checkbox yes Data checkbox yes

    Update: asarih beat me to it. With a better solution too, imo.

    If things get any worse, I'll have to ask you to stop helping me.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found