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 :)


In reply to Re: regex to split a line by McDarren
in thread regex to split a line by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.