Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: hash/array

by Gloom (Monk)
on Jan 26, 2001 at 01:33 UTC ( #54367=note: print w/replies, xml ) Need Help??

in reply to hash/array

This sample of code show you ( I hope ) how to store a table in a hash of tables indexed by its first col.
#!/usr/bin/perl -w use strict; my $data; # open file open FILE , "< /home/you/your.file"; # extract data and store it in a hash of tables while( <FILE> ) { @_ = split; $_ = shift @_; $data->{$_} = [@_]; } # close file ;) close( FILE ); # display result foreach( keys %{$data}) { print $_ , "\t" , join "\t" , @{$data->{$_}} , "\n"; }
sorry if it's more confusing than helpfull but I don't see an easyer way to do that :)

Update :

Well, now i see clearer ...
use enlightment; # ( thx clintp :)

Replies are listed 'Best First'.
Re: Re: hash/array
by clintp (Curate) on Jan 26, 2001 at 03:16 UTC
    open FILE , "< /home/you/your.file";
    Bad form! When yer dealing with a newbie, it's best to show off those things in canonical form:
    open FILE, "/home/you/your.file" or die "Cannot open: $!";
    And as long as we're being picky, the extra indirection with the reference isn't needed. And that join gives an extra tab. And some more code-bumming... (UNTESTED!)
    #!/usr/bin/perl -w use strict; my %data; open FILE , "home/you/your.file" or die "Cannot open: $!"; # extract data and store it in a hash of tables while( <FILE> ) { @_=split; $data{$_[0]} = [ @_ ]; } close FILE; foreach( keys %data) { print join("\t" , @{$data{$_}}), "\n"; }
      What is good for the goose is good for the gander.

      You should do like perlstyle says and include all relevant information in the error message. In this case that includes the filename:

      my $file = "home/you/your.file"; open (FILE, "< $file") or die "Cannot read '$file': $!";
      If this appears within a function I would normally use Carp and then confess to the sad situation...
        i'm restating my problem, because i tried out each of the suggestions, and i couldn't get where i need to go with any of them. i am very much a beginner and am working hard on this, but spinning my wheels - i appreciate the help.

        i have a table like this: rowid course1 course2 course3 course4 marge psychology math engineering pe roy math reading art cooking etc. one of these fields will have lots of text with line breaks. i have a form. the form will send a request to a cgi, passing a value + which matches a value in "rowid" from the table. i need to open the file that contains the table, find the row that mat +ches the value passed by the form, read that row into an array (or ha +sh?) and print it out in html. i'm able to open the file and put it into an array and print it, but i + don't know enough perl yet to take just the row i want and assign la +bels to each value.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://54367]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-01-20 03:02 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (56 votes). Check out past polls.