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

Hi, I have an array @allow_edit - it contains the following info:
binary_app=/usr/bin/true config_file=/etc/inetd.conf MOD_NAME="ABC"

Note that the array contents change

I need to print the array into the form, split the strings at the =, & print up with the following style for each line in the array:

print "<input type=text name =\"$firstpartofsplit\ "binary_app" value= +\"$secondpartofsplit\">\n";

Can anyone offer an example of how to do this please. Many thanks

Replies are listed 'Best First'.
Re: Print array, splitting into 2 strings
by Samy_rio (Vicar) on Mar 24, 2006 at 09:13 UTC

    Hi hmag, Try this,

    use strict; use warnings; while (<DATA>){ chomp; my ($firstpartofsplit, $secondpartofsplit) = split /=/, $_; print "<input type=text name =\"$firstpartofsplit\" value=\"$secondpar +tofsplit\">\n"; } __DATA__ binary_app=/usr/bin/true config_file=/etc/inetd.conf MOD_NAME="ABC"

    You should view How (Not) To Ask A Question

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Print array, splitting into 2 strings
by BrowserUk (Patriarch) on Mar 24, 2006 at 09:20 UTC

    my @array = qw[ binary_app=/usr/bin/true config_file=/etc/inetd.conf MOD_NAME="ABC" ];; print qq[<input type=text name="$_->[0]" value="$_->[1]">\n] for map{ [ split '=' ] } @array;; <input type=text name="binary_app" value="/usr/bin/true"> <input type=text name="config_file" value="/etc/inetd.conf"> <input type=text name="MOD_NAME" value=""ABC"">

    Though browsers probably won't like the doubled, doublequote value in the last line. If pre-quoted values are a part of the data, you might need something like:

    (Updated to correct C&P error).

    print qq[<input type=text name="$_->[0]" value="$_->[1]">\n] for map{ [ m[^([^=]+)=["']?(.*?)["']?$] ] } @array;; <input type=text name="binary_app" value="/usr/bin/true"> <input type=text name="config_file" value="/etc/inetd.conf"> <input type=text name="MOD_NAME" value="ABC">

    Of course, if the values can contain quotes of either form, or you need to retain the quotes around the values, then you might get away with something like

    print qq[<input type=text name="$_->[0]" value="$_->[1]">\n] for map{ [ map{ tr["][']; $_ } split '=' ] } @array;; <input type=text name="binary_app" value="/usr/bin/true"> <input type=text name="config_file" value="/etc/inetd.conf"> <input type=text name="MOD_NAME" value="'ABC'">

    Or you might have to get more sophisticated, or use an HTML or templating module.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Print array, splitting into 2 strings
by holli (Abbot) on Mar 24, 2006 at 09:14 UTC
    my @a = qw(binary_app=/usr/bin/true config_file=/etc/inetd.conf MOD_NA +ME="AB=C"); for ( @a ) { my @s = split (/=/, $_, 1); print qq(<input type=text name ="$s[0]" value="$s[0]">\n); }
    Note, that there is a problem with your quoting in the 3rd element ("ABC").


    holli, /regexed monk/
Re: Print array, splitting into 2 strings
by reasonablekeith (Deacon) on Mar 24, 2006 at 11:20 UTC
    no-one's given a printf example yet. I think it's a bit easier to read, without all the quotes and temporary variables...
    my @array = ( 'binary_app=/usr/bin/true', 'config_file=/etc/inetd.conf', 'MOD_NAME="ABC"' ); printf qq|<input type=text name="%s" value="%s">\n|, split '=' for @ar +ray;
    as mentioned above, you'll need to do something about those double quotes. Here's an example that will just swap all double quotes to singles...
    printf qq|<input type=text name="%s" value="%s">\n|, map {s/"/'/g; $_} + split '=' for @array;
    ---
    my name's not Keith, and I'm not reasonable.
Re: Print array, splitting into 2 strings
by hmag (Acolyte) on Mar 24, 2006 at 09:42 UTC
    Thanks for the replies - I should have noted (& now have) that the contents of the array will change, depending on the file I will be opening (this is only a sample for me to start working off) - hopefully someone can make a suggestion on this
      please read and understand the answers.
        Hi again, Thanks - but if I understood the answers, I would not have reposted - as I mentioned, I am extremely new to Perl, so understanding a lot of the answers is very difficult for me