Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] How do you get a hash to remember things?

Expand Messages
  • Shlomi Fish
    Hi Paul, ... A few more comments on your code ... This should be: open my $long_list,
    Message 1 of 3 , Aug 23, 2010
    • 0 Attachment
      Hi Paul,

      On Sunday 22 August 2010 22:41:24 paulbranon wrote:
      > When my foreach keys loop is within my filereading (<>) while longlist
      > loop, then my hash can remember all its values. But why can't it remember
      > anything as soon as the while loop is finished? (If you assign values to
      > an array you can use them anywhere else in the program) Maybe not with
      > values in a hash? (Or am I forgetting something?)
      > (Or should the question really have been does my hash remember everything,
      > but I'm just not asking it the right questions!?)

      A few more comments on your code

      > open(LONGLIST, "<$databaselonglist") or die "$!\n";

      This should be:

      open my $long_list, "<", $database_long_list
      or die "Could not open file - $!\n";

      1. Lexical file handle.

      2. Three-args-open.

      3. filename variable components separated with underscores.

      4. A more descriptive error.

      It's good that you've thrown an exception though.

      > while (<LONGLIST>) {

      Personally, unless for very quick things - I always do:

      while (my $line = <$long_list>)

      With an explicit variable. $_ can be destroyed too easily.

      > $ItemID="";

      1. Your code needs indentation:


      You should use "my" as well as "use strict;" and "use warnings;".

      > chomp;
      > # You can get the Item-ID split on spaces it's array item[1]
      > @getitemid = split(/\s+/, $_); #print " Item-ID $getitemid[1]\n";

      Separate words with underscores and use my again.

      > @getfileextension=split(/\./, $_); #print " FileExtension

      You should use a regex match to get the file extension instead of a split.

      > $getfileextension[1]\n"; if ( $getitemid[1] =~ /^\d+/){
      > $ItemID = $getitemid[1];
      > }else{
      > }

      Why do you have an empty else ?

      > # this needs to be conditional
      > %hash_table = ('ItemID' => $getitemid[1], 'FileExtension' =>
      > $getfileextension[1]); foreach $key (keys (%hash_table)) { print $key,
      > ",", $hash_table{$key}, "\n"; } #(Prints keynames plus input data)
      > }
      > foreach $key (keys (%hash_table)) { print $key, ",", $hash_table{$key},
      > "\n"; } #(This prints only the key names and no data)

      Your foreach statement should span more lines. And you should use
      "foreach my $key" (see use strict.).

      You should call your hash with a more descriptive name than %hash_table .


      Shlomi Fish

      Shlomi Fish http://www.shlomifish.org/
      Best Introductory Programming Language - http://shlom.in/intro-lang

      God considered inflicting XSLT as the tenth plague of Egypt, but then
      decided against it because he thought it would be too evil.

      Please reply to list if it's a mailing list post - http://shlom.in/reply .
    Your message has been successfully submitted and would be delivered to recipients shortly.