?
Solved

Splitting hash value

Posted on 2003-10-23
3
Medium Priority
?
207 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
[X]
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
  • 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

719 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