Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi Monks

I would like to know how to split multiple patterns or expressions?

For example I have an input file from infos.txt file and I want to split the data there per line

The infos.txt file have the following input:

"Mawts 25,female,melbourne

Awts 24,male,sydney"

I want to split the <Tab> or space between the name and the age, and also split the "commas" (,)

Here's my code but I'm getting different result:
open(INFILE, "<", "infos.txt") or die ("cannot open input: $!"); while (<INFILE>){ chomp; $name; $age; $gender; $address; ($name) = split(" "); print "Name: $name\n"; ($age, $gender, $address) = split(","); print "Age: $age\n"; print "Gender: $gender\n"; print "Address: $address\n"; } exit 0;
The output produced like this:

Name: Mawts 25,female,melbourne

Age: Mawts 25

Gender: female

Address: melbourne

Name: Awts 24,male,sydney

Age: Awts 24

Gender: male

Address: sydney

On the result only the "Name" and "Age" has the wrong output but the "Gender" and "Address" is correct. I know there's something wrong with the code but can't figure it out, I tried combining the splitting patterns but gave me blank results only the commas was seen in the result. Hope you could help guys thanks very much..


In reply to Splitting multiple patterns by astronogun

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-28 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found