Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

regex to split a line

by Anonymous Monk
on Jun 21, 2006 at 03:13 UTC ( [id://556567]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am scraping through my forum's userinfo for another script and I need some help reading the .txt file into a usable hash.

A typical line of the info.txt file is

username Real Name|email@gmail.com|2|30|
I want the hash %users to have their username as the key, and the rest as-is with the delimiters. Without all the extra spaces after the username of course

Can someone help me with this simple regex?

Replies are listed 'Best First'.
Re: regex to split a line
by McDarren (Abbot) on Jun 21, 2006 at 04:22 UTC
    Howdy :)

    Just reading between the lines a little... if you are going to store this stuff in a hash, then I'm guessing that at some later stage you'll want to use the information in the hash. At which time you'll probably need to separate out the different user "attributes" (Real Name, email, etc).

    So, why not do this at the start?

    Because your data appears to be quite well formed, then using split seems the natural way to go, and create a HoH. Something like this:

    #!/usr/bin/perl -w use strict; my %user_info; while (<DATA>) { chomp; my ($user, $attribs) = $_ =~ m/^(\S+)\s+(.+)$/; my ($realname, $email, $foo, $bar, undef) = split /\|/, $attribs; $user_info{$user}{'Real Name'} = $realname; $user_info{$user}{'Email'} = $email; $user_info{$user}{'Foo'} = $foo; $user_info{$user}{'Bar'} = $bar; } __DATA__ username Real Name|email@gmail.com|2|30| fred Fred Bloggs|fbloggs@gmail.com|3|27| harryp Harry Potter|harry@gmail.com|5|32|

    You could then do something like this:

    foreach my $user (keys %user_info) { print "Username:$user Real Name:$user_info{$user}{'Real Name'}\n"; }

    Which would give you:

    Username:harryp Real Name:Harry Potter Username:fred Real Name:Fred Bloggs Username:username Real Name:Real Name

    Note that the above code could be shortened significantly, but I've deliberately kept it verbose so that it's obvious what's going on. It also doesn't include any sanity-checking on your data, but that could easily be added in.

    Hope this helps,
    Darren :)

      This looks to me like a good opportunity to use a hash slice.

      use strict; use warnings; my %user_info; my @fields = qw(name email foo bar); while (<DATA>) { chomp; my ($user, $attribs) = split /\s+/, $_, 2; my %record; @record{@fields} = split /\|/, $attribs; $user_info{$user} = \%record; } foreach my $user (keys %user_info) { print "Username:$user Real Name:$user_info{$user}{name}\n"; }
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: regex to split a line
by bobf (Monsignor) on Jun 21, 2006 at 03:24 UTC

    This should satisfy your requirements, assuming you didn't leave any tricky stuff (like leading whitespaces) out of your OP. Either way, you might want to make sure the regex matched before assigning anything. I assumed that there were a variable number of spaces between the username and the data, since the OP didn't contain a tab.

    use warnings; use strict; my $line = 'username Real Name|email@gmail.com|2|30|'; my ( $name, $data ) = $line =~ m/^(\S+)\s+(.+)$/; print "[$name][$data]";
    If you had a %users hash, you could then do $users{$name} = $data.

    HTH

Re: regex to split a line
by davorg (Chancellor) on Jun 21, 2006 at 07:36 UTC

    Lots of people seem very keen to give you what you asked for (a regex) without pointing out that this is a perfect opportunity to use split (with its optional third argument).

    $data = 'username Real Name|email@gmail.com|2|30|'; my ($key, $val) = split /\s+/, $data, 2; $users{$key} = $value;
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: regex to split a line
by sulfericacid (Deacon) on Jun 21, 2006 at 03:25 UTC
    Hi.

    That looks like a tab to be so this is pretty easy.

    # assuming @users contains your records of the text file my $userinfo; foreach my $user (@users) { my ($first, $last) = split(/\t/, $user); $userinfo{$first} = $last; }
    This is how I'd do this, assuming the username is tab delimited.


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found