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

I am attempting to loop through a hash, and join the elements into a string based on whether or not they are defined. This string is a query for a remedy system, and has some pretty strict rules. If there is more than one element that is defined in the hash, I need to join all the elements via an AND.

I have attempted to do this with join, but the code I was using was adding an AND at the end of the first element, and possibly the last one. I guess I COULD do it like this, and then just use a regex after the loop to strip out the final AND, but I would prefer not to do it this way.

This is my code so far, the hash is built like (actually will be grabbing from CGI - assuming I can get this back end going properly.
$parms{"Login Name+"}="AUSER"; $parms{"Submitted By"}=""; $parms{"Summary"}="test test";
Then I build the query so far like:
my $query; foreach (keys %parms){ $query .= "'$_ '" . " = " . '"' . $parms{$_} '"' . " if $parms{$_} +; }
Which results in a string like 'Login Name+' = "AUSER" (assuming only Login Name element defined). I would need to join any given number of defined elements, based on 15 or so possible keys. Resulting in a string like (based on example above) 'Login Name+' = "AUSER" AND 'Summary' = "test test"

Replies are listed 'Best First'.
(ar0n) Re: Joining, but skipping the last element
by ar0n (Priest) on Jun 29, 2001 at 19:22 UTC
    I'm not sure I understand the problem. What's wrong with join?
    my $query = join " AND ", map { "'$_' = \"$params{$_}\"" } grep $param +s{$_}, keys %params;


    ar0n ]

      Ahh, when used like that, absolutely nothing :-)
      I was using it in the loop like $query=join "AND" , "'$_'=\"$params{$_}\"" , $query Fairly obvious where my error was there..
Re: Joining, but skipping the last element (boo)
by boo_radley (Parson) on Jun 29, 2001 at 19:37 UTC
    the_slycer says :
    I have attempted to do this with join, but the code I was using was adding an AND at the end of the first element, and possibly the last one.
    it sounds like there's an empty element at the end of your list, resulting in an extra "and" at the end. may I suggest grepping? In the example, the first join shows the result of joining with a trailing empty element. The second one shows a way to join on grep, ensuring only non-blank (full?) elements get joined.

    my @list = ("1","2","3",""); print join " and ", (@list); print "\n\n"; print join " and ", grep {$_?$_:undef} @list;

    ooh! also, you could try an array slice! Of course, the above may be better for stripping out any nonblanks, but if you really just want to join an array sans the last element :

    print join " and ", (@list[1,-2]);

Re: Joining, but skipping the last element
by davorg (Chancellor) on Jun 29, 2001 at 19:26 UTC

    I think this does what you want:

    #!/usr/bin/perl -w use strict; my %parms; $parms{"Login Name+"}="AUSER"; $parms{"Submitted By"}=""; $parms{"Summary"}="test test"; my $str = join ' AND ', map { qq/'$_'="$parms{$_}"/ } grep { $parms{$_} } keys %parms; print $str;

    Update: Just too slow there :(

    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>

Re: Joining, but skipping the last element
by bikeNomad (Priest) on Jun 29, 2001 at 19:27 UTC
    You can still use join:

    use strict; my %parms; $parms{"Login Name+"} = "AUSER"; $parms{"Submitted By"} = ""; $parms{"Summary"} = "test test"; print join ( ' AND ', grep { $_ !~ /""$/ } map {"'$_'=\"$parms{$_}\""} keys(%parms) );