Solved

Splitting hash value

Posted on 2003-10-23
3
174 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input from stdin for perl 6 118
perl CPAN issue 3 99
Perl Script - Remove column of data based on column value 2 50
Perl Frameworks 1 81
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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now