http://qs321.pair.com?node_id=55326


in reply to push

facdata is a hash, so push isn't appropriate. You don't need to push anything onto the hash; near as I can tell, what you want to do is to set the keys of %facdata as the values of @facfields, and the corresponding values to the values in @facrow

You're very close indeed :

$facdata{@facfields} = @facrow (a hash slice) seems to be what you want.

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
Re: Re: push
by malaga (Pilgrim) on Jan 31, 2001 at 04:12 UTC
    the thing without the push, as you said, it's working fine, except that i need it to continue through the rows, and get each row that matches. i don't know how to do that (it seems like using foreach is the thing, but can't get it to work), or where to put the rows - push them all into one array, or into different arrays - but then i don't know how to name them.

    i've looked through all my reference books, and online, and can't find anything exactly like this. either i just don't know the syntax well enough or i'm approaching it wrong. for example, should i make this a sub routine because of the my statements?

    thanks for the help.

      I'm not seeing the problem, unless it has to do with that last at the end of the conditional within the loop that traverses the file. The way your code above is written, you will only ever get one entry, because the last breaks out of the while.

      Conceptually, it seems you want a list of the lines in the file that match your criterion, so you definitely should use a single array to hold them all. The way you've written things so far, you want to store those lines not as simple strings (which may or may not be what you should do, depending on the overall aim) but as hashes. Perl doesn't directly support multi-dimensional arrays, or arrays of hashes, but it lets the programmer do it by giving you the ability to store references to any kind of variable any place where you can use a scalar.

      To figure out what the implications of that are, and how to use the tools Perl gives you, you're going to need to consult the documentation I pointed to in my posts above. The basic idea: arrays hold scalars; references are a special type of scalar -- you can think of them as arrows that point to data structures (such as hashes), so what you need to do when you've got a value that is a reference is to get at the thing the reference points to; there are a number of ways of doing that, but here's not the place to duplicate the documentation you have available to you.

      To perform an operation on every element of an array (such as printing out the elements -- NOTE this won't work as you expect if the members of the array are references) , you will almost always want to use foreach.

      HTH

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Re: push
by malaga (Pilgrim) on Jan 31, 2001 at 00:01 UTC
    i need to keep search the rows because there will be more than one with the value i'm looking for. can i do a while and a foreach in the same statement?

      If you may have more than one value that you want to save, I'd use an array of (references to) hashes; at least, sticking with the structure you've already got. For that, you're going to need to read up on references, so perldoc perlref and perldoc perlreftut are your starting points. something like the following should get you started:

      my @lines; # ... as before # instead of *your* push line, @facdata{@facfields} = @facrow; push @lines, \%facdata;

      That will leave you with an array of references to hashes. This is a little hairier than the structure you might want (you could, e.g. just store an array of strings, and call the splits on those later. But whatever ...)

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        i tried it that way and i get an error that it needs an explicit package. do you know which pm it might need?
        thanks...i'll try again.