Welcome to WebmasterWorld Guest from 54.163.35.238

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Parsing data - how to find word position

...and then return a column of data

     

MichaelBluejay

2:29 pm on Mar 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to return a specific column of data based on the user's input. I figured out a way to do it but it seems rather awkward and inefficient. Is there a better way?

$data = q[ 
smith blue apple cat
jones red grape dog
clark green banana mouse
];

$input = 'Fruit';
@headings= qw(Name Color Fruit Animal);
$counter=0;
for $item (@headings) {
if ("$item " eq "$input ") { $whichColumn=$counter; last;}
$counter++;
}

@data = split("\n",$data);
for $item (@data) {
@columns = split (' ',$item);
print "$columns[$whichColumn]<p>";
}

perl_diver

7:31 pm on Mar 27, 2006 (gmt 0)

5+ Year Member



is there any reason why you aren't using a hash?

bennymack

12:00 am on Mar 28, 2006 (gmt 0)

10+ Year Member



Try using List::MoreUtils::firstidx. It's not a standard module so you may have to install it. Once that's take care of you'd use it like:

<untested>


use List::MoreUtils qw[firstidx];

$data = q[
smith blue apple cat
jones red grape dog
clark green banana mouse
];

$input = 'Fruit';
@headings= qw(Name Color Fruit Animal);
$whichColumn = firstidx{$_ eq $input}@headings;

@data = split("\n",$data);
for $item (@data) {
@columns = split (' ',$item);
print "$columns[$whichColumn]<p>";
}

</untested>

perl_diver

6:14 am on Mar 28, 2006 (gmt 0)

5+ Year Member



another way:


my $data = q[
smith blue apple cat
jones red grape dog
clark green banana mouse
];
# remove leading and trailing spaces in lines
$data =~ s/^\s+//gm;
$data =~ s/\s+$//gm;
my $input = 'Fruit';
my @headings= qw(Name Color Fruit Animal);
for my $i (0..$#headings) {
next if ($headings[$i] ne $input);
print map {(split(/\s+/))[$i]."\n"}
map {split(/\n/)} $data;
last;
}

MichaelBluejay

5:42 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the help.

As for why I wouldn't use a hash, the real table has 166 columns and 100 rows. And there are about a dozen different tables. I want to be able to just dump these space-delimited tables into my code, without having to write yet more code to extract the columns, in order to have code that prints the columns based on the user's input.

Anyway, from your examples it looks like I wasn't too far off the mark -- Perl just doesn't seem to have a super-efficient way to refer to the Nth word of a line. I guess I'm spoiled by Xtalk, where you can write code like "put word 5 of line 3 of theVar into word 2 of line 6 of someOthervar".

perl_diver

6:40 pm on Mar 28, 2006 (gmt 0)

5+ Year Member



perl runs through arrays very fast so your way or my way or the way bennymack suggested are probably all pretty efficient. XTalk might be able to use a seemingly simple line of code to do what you want but behind the scenes it's probably doing something similar to what perl is doing: looping through the data to get what it needs. Although I could be wrong since I don't know anything about XTalk. But really, your code was pretty good to begin with.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month