Assuming you want to ignore the 'asdgjhashdnh gsdh sjs klja' stuff and only extract single-digit/alpha-string pairs, this might be a way to go (sorry for the wrap-around):
>perl -wMstrict -le
"my $s = '1 human 2 flower 3 fruits 5 human 6 car 9 flower asdgjhashdn
+h gsdh sjs klja';
my %hash = $s =~ m{ (\d) \s+ ([[:alpha:]]+) }xmsg;
use Data::Dumper;
print Dumper \%hash;
"
$VAR1 = {
'6' => 'car',
'1' => 'human',
'3' => 'fruits',
'9' => 'flower',
'2' => 'flower',
'5' => 'human'
};
One problem with
my ($index, $data)=split (/\s+/,$line);
is that it only captures the first index/data pair, and the example in the OP has several pairs per line. (Update: This is actually pointed out by moritz.) Another problem is that the extraneous data (the 'asdgjhashdnh gsdh sjs klja' stuff) would not be filtered out by a split that simply captured all the field pairs generated.
-
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.
|