Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Splitting hash value

Posted on 2003-10-23
3
189 Views
Last Modified: 2010-07-27
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
Comment
Question by:mmcw
  • 3
3 Comments
 
LVL 3

Expert Comment

by:BioI
ID: 9607927
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
 
LVL 3

Expert Comment

by:BioI
ID: 9607956
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
 
LVL 3

Accepted Solution

by:
BioI earned 125 total points
ID: 9608028
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question