Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Remove & from file

by Jamison (Initiate)
on Mar 17, 2004 at 03:03 UTC ( #337202=perlquestion: print w/replies, xml ) Need Help??
Jamison has asked for the wisdom of the Perl Monks concerning the following question:

If I have a script to read a file that is like so: &amount=100.00&name=jim&address=soandso How do I remove the " & " . Here is what I did so far
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); print "Content-type:text/html\n\n"; open(INF,"data.txt") or dienice("Couldn't open survey.out for reading: + $! \n"); @data = <INF>; close(INF); foreach $i (@data) { chomp($i); ($info) = split(/\ /,$i); # Now we can print out a web page summarizing the data. print"<html><head><title>Input from Visitors</title></head>"; print "<body>"; print " Data is: "; print $info; print "<br><hr><br>"; print "</body></html>"; sub dienice { my($msg) = @_; print "<h2>Error</h2>\n"; print $msg; exit; } }

Replies are listed 'Best First'.
Re: Remove & from file
by graff (Chancellor) on Mar 17, 2004 at 04:11 UTC
    Well, just in terms of cosmetics, let me suggest that you place the subroutine definition outside the foreach loop -- it just makes more sense that way, and is less confusing.

    As for the procedure, you say the input file contains (one or more?) line(s?) like "&name1=value1&name2=value2&...", and if that is true, can you say what this line of coding is doing, and why you're doing this:

    ($info) = split(/\ /,$i);
    I'm not sure (anything containing strings of two or more white-space characters is visually ambiguous), but I think:
    • The backslash is unnecessary
    • You're only saving the part before the first white-space string into $info (is that your intention?)

    As for your stated purpose (to "remove &"), that's not entirely clear either. If you just remove this character from the input, you'll get (one or more?) strings like "name1=value1name2=value2name3=value3...", and I don't think that's what you want.

    Maybe you mean "how do I split the line at the "&" characters?" If so, you do it like this:

    my %parhash; for my $i (@data) { chomp $i; my @params = split( /\&/, $i ); # and while you're at it, the "nameX=valueX" can be # stored in a hash like this: for ( @params ) { next unless ( /.=./ ); # make sure there's a name and a value my ($k,$v) = split(/=/); $parhash{$k} = $v; } } # print html header stuff, then: print "user supplied data was:<br>\n"; for ( sort keys %parhash ) { print "$_ = $parhash{$_}<br>\n"; } # print html trailer stuff
Re: query string from file
by Kozz (Friar) on Mar 17, 2004 at 03:50 UTC

    I'd recommend you try using the CGI module. When you create your CGI object, you just pass the query string to the constructor.

    Do this, and you've got all the key/value pairs nicely split up and inside your CGI object, not to mention any URL-escaped characters have been converted. Get the keys using the param() method w/o an argument.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://337202]
Approved by graff
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2017-02-23 14:41 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (347 votes). Check out past polls.