Solved

Splitting hash value

Posted on 2003-10-23
3
162 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 184
MySQL Web Database 5 105
perl script help 12 101
Having a problem with ActiveState Perl on 64-bit Windows 7 3 61
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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

763 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

10 Experts available now in Live!

Get 1:1 Help Now