Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Splitting hash value

I am using the folloing script:

#!/usr/bin/perl -w

%credit_form_fields_extra = (
      "Cash in advance" => "",
      "Send a check" => "",
      "Authorize for once" => "Bank Account|test",
      "Cash on delivery" => "",
      "By credit card" => "Card Type|Card Holder Name|Card Number|Exp Month|Exp Year"
);

print "Content-Type: text/html\015\012\015\012";

foreach (keys %credit_form_fields_extra) {
      if ($_ eq "Authorize for once") {
            my @test = split("|",$credit_form_fields_extra{$_});
                  
            foreach my $test (@test) {
                  print "a: $test<br>";
            }
      }
}

1;

I want to split the $credit_form_fields_extra{'Authorize for once'} to an array @test.
The loop:

foreach my $test (@test) {
print "a: $test<br>";
}

has to return:

a: Bank Account
a: test

But it returns:

a: B
a: a
a: n
a: k
a:
a: A
a: c
a: c
a: o
a: u
a: n
a: t
a: |
a: t
a: e
a: s
a: t

What am I doing wrong?
0
mmcw
Asked:
mmcw
  • 3
1 Solution
 
BioICommented:
This works for me ...

#!/usr/bin/perl -w

%credit_form_fields_extra = (
     "Cash in advance" => "",
     "Send a check" => "",
     "Authorize for once" => "Bank Account|test",
     "Cash on delivery" => "",
     "By credit card" => "Card Type|Card Holder Name|Card Number|Exp Month|Exp Year"
);

print "Content-Type: text/html\015\012\015\012";

foreach (keys %credit_form_fields_extra) {
     if ($_ eq "Authorize for once") {
          my @test = split(/\|/,$credit_form_fields_extra{$_});
               
          foreach my $test (@test) {
               print "a: $test<br>";
          }
     }
}

1;
0
 
BioICommented:
What I changed is the pattern where you want to split: instead of "|" , you should write /\|/ because "|" is  a meta-character.
I changed:
my @test = split("|",$credit_form_fields_extra{$_});
in:
my @test = split(/\|/,$credit_form_fields_extra{$_});

this gave as ouput [!! if you add "\n" in print -> print "a: $test<br>\n";  :
a: Bank Account<br>
a: test<br>
0
 
BioICommented:
I assume you also want to have a new line when you add <br> to print?  
In perl, you will have to add "\n" instead of "<br>"



#!/usr/bin/perl -w

%credit_form_fields_extra = (
     "Cash in advance" => "",
     "Send a check" => "",
     "Authorize for once" => "Bank Account|test",
     "Cash on delivery" => "",
     "By credit card" => "Card Type|Card Holder Name|Card Number|Exp Month|Exp Year"
);

print "Content-Type: text/html\015\012\015\012";

foreach (keys %credit_form_fields_extra) {
     if ($_ eq "Authorize for once") {
          my @test = split(/\|/,$credit_form_fields_extra{$_});
               
          foreach my $test (@test) {
               print "a: $test\n";
          }
     }
}

1;
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now