in reply to getting the right stuff

i thought i had it all! but noooooooo.

i need to print more than one 'item' (where the *****'s are). what am i doing wrong?
my $value = param('name');#the value passed from the webpage my %data = (); my @fields = split(/\t/, <FILE>); chomp @fields; my @records; my %ref; while(<FILE>) { chomp; my @row = split(/\t/); if ($row[0] eq $value) { my %data; @data{@fields} = @row; push @records, \%data; } } close (FILE); print "<ul>"; foreach my $ref ( @records ) { #print $ref->{'FirstName'}, "<br>"; *****i'm using this when there + is only one thing to print print @{$ref}{qw/FirstName MiddleName LastName}; *****i was told t +o use this for more than one, but can't make it work. } print "</ul>";

Replies are listed 'Best First'.
Re: Re: getting the right stuff
by dkubb (Deacon) on Feb 07, 2001 at 12:53 UTC
    In your second print statement, there is no "/" between LastName and the curly brace. You'll need to close it like this:

    print @$ref{ qw/FirstName MiddleName LastName/ };

    If that doesn't solve your problem, look into the way you are constructing each hashref in the first loop. Maybe the keys in @fields aren't correct? Data::Dumper will be your friend in times like these. Use it to peer inside @results.

    Also, a side note, you may be able to simplify and/or speed up the parsing routine you have using DBD::CSV along with DBI. Not only is the underlying engine it uses faster than regexes, but it handles all the parsing details for you. Another benefit is it provides you a little less painful upgrade path in the future if you need to move to a relational database.

    Here's an example of what you were doing, only using DBI:

    #Connect to the data source my $dbh = DBI->connect( "DBI:CSV(RaiseError=>1,AutoCommit=>1,Taint=>1,ChopBlanks=>1):csv_sep +_char=\t" ); my $statement = q{ SELECT * FROM filename WHERE FirstName = ? }; my $sth = $dbh->prepare($statement); $sth->execute($value); while(my $row = $sth->fetchrow_hashref) { print @$row{ qw(FirstName MiddleName LastName) }; }

    Make sure that the word "filename" inside $statement gets changed to whatever file you're parsing. You will need to rename the file so that it has no extension, as DBD::CSV requires this.

    Update: I shouldn't have used SELECT * like above, this requests all the fields, not just what I need. In some cases using * in SQL can be very evil when you've got alot of records. The general rule of thumb is you should try to SELECT only those columns that are necessary. Sorry, my bad.

    It should read like this:

    my $statement = q{ SELECT FirstName , MiddleName , LastName FROM filename WHERE FirstName = ? };
      thanks a lot for the help!we can't get the server guys to put dbi on right now. it's a future type of thing. but i might try it on a different server just for practice. putting the slash in gives me the first and last item, but leaves out the middle one. do i need anything else in between those?

        I can't see anything physically wrong with the code you last posted. I would confirm in your data file that the MiddleName is even specified. Another possibility is the datasource isn't properly formatted, it would be very easy to miss a tab.

        You should start at the first point when you get the data and trace it through the execution of your program. Before and after each step where it could be modified, you should check it and make sure it's correct. The debugger or a few print statements should help you trace where the problem is.

        By the way, in the last code you posted, there were two lines of code that weren't being used for anything, as far as I could determine, the declaration of %data and %ref.