Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Regex During Map Function

by deMize (Monk)
on Dec 15, 2010 at 14:33 UTC ( [id://877279]=perlquestion: print w/replies, xml ) Need Help??

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

Question: How do you perform regex on the string generated by the join function before assigning it as a hash value?

%params = map { $_ => join("; ", split("\0", $cgi->Vars->{$_})) } $cgi->param;

There may be a better way to do this, but I'm getting some strings like "; foo; bar; foobar; baz", which leads me to believe that there is a null byte being sent. I'm trying to perform s/^\;\s+// on the result of join inside the map function.



Update: Here's the effect I'm going for, but I didn't know if there was a way to write something as concise, but more efficient
%params = map { $_ => do {my $t=join("; ", split("\0", $cgi->Vars->{$_}));$t=~s/^\;\s+//;$t;} } $cgi->param;



Thank you for your help.


Demize

Replies are listed 'Best First'.
Re: Regex During Map Function
by Anonymous Monk on Dec 15, 2010 at 14:41 UTC
    Well thats just all kinds of sideways :) Either use param or use Vars, but not both
    my %params = map { my $vv = join ';', $cgi->param($_); # do stuff with $vv $_ => $vv } $cgi->param;
      This is legacy code and I remember at the time there was a reason I did both. I think you're right though. I have to see how I'm doing this in the new way.

      Update: I remember why. CGI->param does not return multi-valued options, unless I'm missing the correct way to do it. I'm not sure why using one would impact performance in any way. Aren't they both already instantiated in memory? My guess would be that I should use the CGI->Vars and its keys.
Re: Regex During Map Function
by samarzone (Pilgrim) on Dec 15, 2010 at 14:52 UTC

    You can use grep to filter out empty values

    %params = map { $_ => join("; ", grep {$_} split("\0", $cgi->Vars->{$_})) } $cgi->param;

    -- Regards - Samar

      Remember, however, that '0' is false, hence will be filtered out. Better, perhaps, to use
          grep { length $_ }

      I don't want to filter out all empty values, though. Just the leading of a multi-valued element.

      If a form control has a bunch of checkboxes w/ the same element name, I want them all. I also want the blank values for those elements that don't have multiple values.

      For some reason there's an empty value for a multi-valued list control.
Re: Regex During Map Function
by Anonymous Monk on Dec 15, 2010 at 15:17 UTC
    my %params = $cgi->Vars; $_ = join('; ', split("\0", $_)), s/^;\s*// for values %params;
Re: Regex During Map Function
by wagnerc (Sexton) on Dec 15, 2010 at 17:48 UTC
    Yes, u've hit the age old problem of s/// not returning the bound value but the substitution count. To get the value returned in place use this little hack:
    ($var =~ s/foo/bar/ ? $var : $var)
    This way the post op value of $var will always be returned. HTH.
      Use do to use a block containing several statements in an expression:
      do { s/foo/bar/; $_ }
      Although, once you're using map, you don't need do: just use the block. And no comma.
      That's true, but it'd require declaring the $var before the map. I was hoping to do it without the additional variable declaration outside the map function. It'd probably be more efficient to do it outside, though. The real hope was to apply it to the join result, rather than creating a variable at all.

      Thank you


      Demize

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-19 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found