Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

hashes

by jrsmith (Pilgrim)
on May 31, 2000 at 21:49 UTC ( [id://15660]=perlquestion: print w/replies, xml ) Need Help??

jrsmith has asked for the wisdom of the Perl Monks concerning the following question:

i accidentally hit the new button a little too early, sorry for the duplicate posts... i have a subroutine which does some processing on html files and stores values from those files into a hash. now up until recently i had made it a habit not to use -w or use strict in my scripts, but well i just decided i oughta start :) so anyway, i have this hash that looks like this:
sub processhtml { my ($file) = $_[0]; my ($filename, $i, $value2, $value3); my (@input, @values); ... substitution and pattern match stuff ... @values = split(" ", $i); if ($values[2] !~ /^[a-z]{1,2}$/i) { $values[2] = ""; } else { } if ($values[0] !~ /000/) { $import_values{"ship"} = $file; $import_values{"weight"} = $values[0]; $import_values{"dwg"} = $values[1]; $import_values{"rev"} = $values[2]; foreach $value2 (keys %import_values{"ship","weight","dwg","rev +"}) { print TEMP ("\"$value2\","); } print TEMP ("\n"); } else { $import_values{"ship"} = $file; $import_values{"weight"} = ""; $import_values{"dwg"} = $values[1]; $import_values{"rev"} = $values[2]; foreach $value3 (keys %import_values{"ship","weight","dwg","rev +"}) { print TEMP ("\"$value3\","); } print TEMP ("\n"); } }
now the problem lies in the foreach loops, i keep getting the error "Can't use subscript on private hash" and i don't know why... as weird as it may sound prior to applying use strict and -w i had array symbols preceding them
foreach $value3 (keys @import_values{"ship","weight","dwg","rev +"}) { print TEMP ("\"$value3\","); }
and it worked, but now this problem is driving me up the wall... can anyone shed any light on this for me?

Replies are listed 'Best First'.
RE: hashes
by nuance (Hermit) on May 31, 2000 at 22:02 UTC
    I think you probably want

    foreach $value2 ("ship","weight","dwg","rev") { print TEMP ("\"$import_values{$value2}\","); } print TEMP ("\n");
    or something approximating that.

    Nuance

    Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"

RE: hashes
by neshura (Chaplain) on May 31, 2000 at 21:55 UTC
    is there a reason you can't just do
    foreach $value3 (keys %import_values) { print TEMP ("\"$value3\","); }
    i've never seen anyone use that hash construct you've got in the foreach, but i'm a relative newbie...

    e-mail neshura

      i need the values to be printed out in that exact order, and if i didn't specify the keys they would be random..
        print OUT $hash{key1}; print OUT $hash{key2}; etc...

        If you want them in a specific order, use them in that specific order. Or do something like:

        my @keys = ('one', 'two', 'three'); for (@keys) { print OUT $hash{$_}; }

        Then, you have already predetermined your order, and can add/change it at any time.

        Cheers,
        KM

Re: hashes
by swiftone (Curate) on May 31, 2000 at 22:02 UTC
    Here's the trick:
    the keys function returns an array.
    When you do
    keys @import_values{"ship","weight","dwg","rev"}
    You are causing something very funky
    %import_values{"ship","weight","dwg","rev"}
    Is a syntax error.
    @import_values{"ship","weight","dwg","rev"}
    Is trying to take a hash slice, which works, but returns an array, and keys expects a hash.

    As others have said, just use keys %hash.

    The other (not as good, but preserves order of keys) way would be:

    foreach $value3 (@import{'ship','weight','dwg','rev'})
    See perldata, line 867 or so, for discussion of hash slices
Re: hashes
by jrsmith (Pilgrim) on May 31, 2000 at 22:03 UTC
    is it not a valid practice to specify the keys you want to print when looping through a hash like that? you seem to be surprised that i would do that, is there a better way? i think i may just forego the foreach altogether.. yes now that i think about it it's not really necessary as the hash will not be varying in size..
      The thing is, if you know which keys you want to process, and you want them in a specific order, there is no reason to use keys in the first place. Just give the keys in an array, as others have suggested, and index into the hash using them. keys is useful for getting all the keys in a hash, precisely so that you don't have to know their values in advance.

      --ZZamboni

        yeah i realize that now.. keys isn't really useful here.. but hey i wrote this script after maybe 2 or 3 weeks of teaching myself perl and now having had 2 whole months of experience i actually have learned a few things.. thanx :)
      is it not a valid practice to specify the keys you want to print when looping through a hash like that?

      No, unfortunately you can't specify the order that keys will be returned, not using that method anyway. You can either use the keys function and sort its return value, or specify each of the keys in some other way, see posts above for examples

      Nuance

      Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"

Re: hashes
by cwest (Friar) on May 31, 2000 at 23:37 UTC
    my %hash = ( one => 1, two => 2, three => 3 ); print $hash{$_} foreach qw/ one two three /;
    --
    Casey
    
Re: hashes
by KM (Priest) on May 31, 2000 at 21:59 UTC
    Why are you using the hash like that? Why not juse:

    for (keys %hash) { ... whatever ... }
    ??

    Cheers,
    KM

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://15660]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found