Oh, did you change $in{my} to $data{my} in those examples I gave?
Feb 8, 2002, 4:23 AM
Veteran (19537 posts)
Feb 8, 2002, 4:23 AM
Post #28 of 48
Views: 8872
Ok,
Could you try getting rid of the local $, bit and change the split part to split by \0 rather than |
If that doesn't work I'd need to make some changes to the get_data routine as I'm not sure it will allow you to do this.
Could you try getting rid of the local $, bit and change the split part to split by \0 rather than |
If that doesn't work I'd need to make some changes to the get_data routine as I'm not sure it will allow you to do this.
Feb 8, 2002, 5:20 AM
User (159 posts)
Feb 8, 2002, 5:20 AM
Post #31 of 48
Views: 8821
Didnt work. I rolled back to my previous solution:
In the form:
<input type=checkbox name=my value="$id,">sdafsdfsdf
Splitting up:
@fields = split(/\,/, $data{'my'});
Checking against db file:
for ($count=0;$fields[$count]>0 ;++$count) {
if ($fields[$count] eq $id) {
push (@result_list, $line)
}
}
This results in the passed values being split correctly into $fields[x] - but only the first $field[0] to be recognised in the [for] loop. Even thought I can see the other matches being correct...
I think this must be a prob with the handling code, ie:
sub get_data {
#######################################################################################
local($string);
# get data
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$string = $ENV{'QUERY_STRING'};
}
else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }
# split data into name=value pairs
@data = split(/&/, $string);
# split into name=value pairs in associative array
foreach (@data) {
split(/=/, $_);
$_[0] =~ s/\+/ /g; # plus to space
$_[0] =~ s/%0D%0A%0D%0A/\n\n/g; #added by kristina make newlines?
$_[0] =~ s/%0a/newline/g;
$_[0] =~ s/\%00//g;
$_[0] =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
if(defined($data{$_[0]})){
$data{$_[0]} .= "\0";
$data{$_[0]} .= "$_[1]";
}
else {
$data{"$_[0]"} = $_[1];
}
}
# translate special characters
foreach (keys %data) {
$data{"$_"} =~ s/\+/ /g; # plus to space
$data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
$data{"$_"} =~ s/\%00//g;
}
%data; # return associative array of name=value
#######################################################################################
}
In the form:
<input type=checkbox name=my value="$id,">sdafsdfsdf
Splitting up:
@fields = split(/\,/, $data{'my'});
Checking against db file:
for ($count=0;$fields[$count]>0 ;++$count) {
if ($fields[$count] eq $id) {
push (@result_list, $line)
}
}
This results in the passed values being split correctly into $fields[x] - but only the first $field[0] to be recognised in the [for] loop. Even thought I can see the other matches being correct...
I think this must be a prob with the handling code, ie:
sub get_data {
#######################################################################################
local($string);
# get data
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$string = $ENV{'QUERY_STRING'};
}
else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }
# split data into name=value pairs
@data = split(/&/, $string);
# split into name=value pairs in associative array
foreach (@data) {
split(/=/, $_);
$_[0] =~ s/\+/ /g; # plus to space
$_[0] =~ s/%0D%0A%0D%0A/\n\n/g; #added by kristina make newlines?
$_[0] =~ s/%0a/newline/g;
$_[0] =~ s/\%00//g;
$_[0] =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
if(defined($data{$_[0]})){
$data{$_[0]} .= "\0";
$data{$_[0]} .= "$_[1]";
}
else {
$data{"$_[0]"} = $_[1];
}
}
# translate special characters
foreach (keys %data) {
$data{"$_"} =~ s/\+/ /g; # plus to space
$data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
$data{"$_"} =~ s/\%00//g;
}
%data; # return associative array of name=value
#######################################################################################
}
Feb 8, 2002, 5:52 AM
Veteran (19537 posts)
Feb 8, 2002, 5:52 AM
Post #32 of 48
Views: 8933
:(
Here's what I use in my scripts to grab form input if it is any help:
#----------------------------------------------------------
# Return a hashref of all params and their values.
my ($INPUT) = {};
my (@INPUT) = ();
for (param()) {
@INPUT = param($_);
# Decide whether it is a single value or multiple.
if (@INPUT > 1) {
@{$INPUT->{$_}} = @INPUT
}
else {
$INPUT->{$_} = param($_)
}
}
return $INPUT;
}
I changed the sub name to get_data incase you want to try it.
Basically it detects whether the input is one value or multiple for each field and handles it appropriately.
So for your $data{my} field it would automatically put the data into an array ready for use.
The only thing you'd need to do to use it would be to add:
use CGI qw(:standard);
...to the top of the cgi script. The original code uses objects but I removed that so you don't need to bother creating one.
So basically you'd do:
my $input = get_data();
...and then hopefully you'll have access to the my field using @{$input->{my}}
Here's what I use in my scripts to grab form input if it is any help:
Code:
sub get_data { #----------------------------------------------------------
# Return a hashref of all params and their values.
my ($INPUT) = {};
my (@INPUT) = ();
for (param()) {
@INPUT = param($_);
# Decide whether it is a single value or multiple.
if (@INPUT > 1) {
@{$INPUT->{$_}} = @INPUT
}
else {
$INPUT->{$_} = param($_)
}
}
return $INPUT;
}
I changed the sub name to get_data incase you want to try it.
Basically it detects whether the input is one value or multiple for each field and handles it appropriately.
So for your $data{my} field it would automatically put the data into an array ready for use.
The only thing you'd need to do to use it would be to add:
use CGI qw(:standard);
...to the top of the cgi script. The original code uses objects but I removed that so you don't need to bother creating one.
So basically you'd do:
my $input = get_data();
...and then hopefully you'll have access to the my field using @{$input->{my}}
Feb 8, 2002, 6:13 AM
Veteran (19537 posts)
Feb 8, 2002, 6:13 AM
Post #34 of 48
Views: 8865
If the field has multiple values then they are accessible using
@{$input->{fielname}}
...or ${$input->{fieldname}}[x] ...for individual fields where x is the element.
...otherwise it will be just $input->{fieldname} for single values.
You may need to have a play to see if/how it works by just printing values.
Just submit your form and on the success page add:
print join("<BR>", @{$input->{my}});
...and see if it print all the values you selected.
@{$input->{fielname}}
...or ${$input->{fieldname}}[x] ...for individual fields where x is the element.
...otherwise it will be just $input->{fieldname} for single values.
You may need to have a play to see if/how it works by just printing values.
Just submit your form and on the success page add:
print join("<BR>", @{$input->{my}});
...and see if it print all the values you selected.
Feb 8, 2002, 8:29 AM
User (159 posts)
Feb 8, 2002, 8:29 AM
Post #35 of 48
Views: 8798
works brilliantly. I can extract the values correctly.... but .... the below still doesnt work... :( even if I can see that
${$input->{my}}[0];
${$input->{my}}[1];
${$input->{my}}[2]; - are correct
for ($count=0;$fields[$count]>0 ;++$count) {
$this=${$input->{my}}[$count];
if ($this eq $id) {
push...
}
}
${$input->{my}}[0];
${$input->{my}}[1];
${$input->{my}}[2]; - are correct
for ($count=0;$fields[$count]>0 ;++$count) {
$this=${$input->{my}}[$count];
if ($this eq $id) {
push...
}
}
Feb 8, 2002, 8:33 AM
Veteran (19537 posts)
Feb 8, 2002, 8:33 AM
Post #36 of 48
Views: 8853
>>
for ($count=0;$fields[$count]>0 ;++$count) {
$this=${$input->{my}}[$count];
if ($this eq $id) {
push...
}
<<
I wouldn't use that. You need to do a while loop through the file like I gave above otherwise you are looping through the input rather than the file and so you can only do as many loops as there are elements in the array which won't give the correct matches.
for ($count=0;$fields[$count]>0 ;++$count) {
$this=${$input->{my}}[$count];
if ($this eq $id) {
push...
}
<<
I wouldn't use that. You need to do a while loop through the file like I gave above otherwise you are looping through the input rather than the file and so you can only do as many loops as there are elements in the array which won't give the correct matches.
Feb 24, 2002, 6:55 AM
Veteran (19537 posts)
Feb 24, 2002, 6:55 AM
Post #46 of 48
Views: 8702
Yes that is correct, assuming I understood what you wanted?
The regex will pick out the second field from every line of the file and store it in $second_field.
If you want all second fields to be put in an array or something you'd need:
/^\d+\|([^|]+)/ and push @array, $1;
The regex will pick out the second field from every line of the file and store it in $second_field.
If you want all second fields to be put in an array or something you'd need:
/^\d+\|([^|]+)/ and push @array, $1;