Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Splitting multiple patterns

by astronogun (Sexton)
on Apr 09, 2012 at 04:35 UTC ( [id://964082]=note: print w/replies, xml ) Need Help??


in reply to Re: Splitting multiple patterns
in thread Splitting multiple patterns

Hi stevieb

How about if I put the infos.txt file into a array first and then splitting it from there. like first it will open the .txt file then putting it in a array, then split them inside..

open(INFILE, "<", "infos.txt") or die ("cannot open input: $!"); my @infos = <INFILE>; chomp(@infos);

How can I split the datas on my @infos? thanks

Replies are listed 'Best First'.
Re^3: Splitting multiple patterns
by stevieb (Canon) on Apr 09, 2012 at 04:48 UTC

    It's really not much different. However, slurping in the whole file can run you out of memory if your file is very large.

    #!/usr/bin/perl use warnings; use strict; open my $fh, "<", "infos.txt" or die "cannot open infos.txt: $!"; my @array = <$fh>; close $fh; for my $line ( @array ){ chomp $line; my ( $name, $rest ) = split /\s+/, $line; my ( $age, $gender, $address ) = split /,/, $rest; print "Name: $name\n"; print "Age: $age\n"; print "Gender: $gender\n"; print "Address: $address\n"; print "\n"; }
      Hi stevieb,

      One last question. How about if want to get only the $address? and only print the "Address:"? What I want is to split the Name up to Gender so that it will only read the $address

      Thanks
        I'm not stevieb, but I think I can help you.
        Update: mis-spelled stevieb's name previously - my goof!
        To just get the address, use a "list slice". Let split do its thing and just select the parts that you want.
        change my ( $age, $gender, $address ) = split /,/, $rest; to my ($address) = (split /,/, $rest)[-1]; or my ($address) = (split /,/, $rest)[2];
        Perl can use a negative index! -1 in this case means "the last thing". index of 2 means 3rd thing from the left. Which one you use is dependent upon both preference and situation. In this case, the results should be identical.

        As another point, if for example the address may contain comma's itself (maybe a street address in addition to the city name), you can limit the split with the 3rd parameter. (split /,/,$rest,3) allows max of 3 things in the split - that way the whole address would appear as the "last thing". BTW: I would use $city instead of $address if this is just a $city name (personal preference because $address implies something far more than just $city to me).

        If this is a more complicated CSV file, with embedded commas which are quoted, using one of the CSV modules to parse this is the way to go - the CSV format is deceptively simple sounding, but the complications escalate dramatically when a comma (the field separator) can appear within one of the data fields.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://964082]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found