homepage Welcome to WebmasterWorld Guest from 54.227.41.242
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Parsing data - how to find word position
...and then return a column of data
MichaelBluejay




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

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




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

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

bennymack




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

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




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

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




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

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




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved