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

Greetings Monks, I have a file that looks something like this:
CS CS
Here is my problem. The current code I have is
open(PBXNUM1, ">pbxnum1"); open(MYINPUTFILE, "pbxnum"); my %hash; while (<MYINPUTFILE>) { chomp; $hash {$_} ++; print PBXNUM1 "$_$hash{$_}\n"; }
Running the above file thru this hash will return
CS1 CS2
While this is great when generating new files, the problem is that CS1 & CS2 already exist, and I need it to begin at CS3. There is a variable, $pbxex, that the user will input to show the number of existing occurances, but no matter how I reference that variable (see below), the hash always begins at "1". Any ideas how I can modify this code to work properly? As usual, I appreciate any and all suggestions.
open(PBXNUM1, ">pbxnum1"); open(MYINPUTFILE, "pbxnum"); my $hash = $pbxex; while (<MYINPUTFILE>) { chomp; $hash {$_} ++; print PBXNUM1 "$_$hash{$_}\n"; }

Replies are listed 'Best First'.
Re: Hash not working as intended
by jethro (Monsignor) on Feb 28, 2011 at 15:26 UTC
    ... chomp; $hash{$_}= $pbxex-1 if (not exists $hash{$_}); $hash {$_} ++; ...

    This is one way, initizalize the hash with your base value whenever you want to increment a value that wasn't accessed yet. In this case the base value is one less than the starting value because there is an increment following the initialization

    Another way would be to add 2 to every value in the hash after you read in all values from the file:

    ... chomp; $hash {$_} ++; } foreach (keys %hash) { $hash{$_}+= $pbxex-1; print PBXNUM1 "$_$hash{$_}\n"; }
      Great, that works perfectly...thanks for the help
Re: Hash not working as intended
by Eliya (Vicar) on Feb 28, 2011 at 15:24 UTC

    If $pbxex is the starting number, you probably want something like this:

    if (exists $hash{$_} ) { $hash{$_}++; } else { $hash{$_} = $pbxex; }
Re: Hash not working as intended
by toolic (Bishop) on Feb 28, 2011 at 15:30 UTC
    You have to show us what $pbxex looks like. If the hash already has something in it, your code seems to work fine for me:
    use warnings; use strict; my %hash = (CS => 4); while (<DATA>) { chomp; $hash{$_}++; print "$_$hash{$_}\n"; } __DATA__ CS CS
    prints:
    CS5 CS6
Re: Hash not working as intended
by runrig (Abbot) on Feb 28, 2011 at 15:32 UTC
    If you're using this to generate filenames, why not check to see if the file exists?
    do { $hash{$_}++ } until ! -f $_;
    Update: Umm, there's a large probability I may have misunderstood what you're trying to accomplish.
Re: Hash not working as intended
by AnomalousMonk (Archbishop) on Feb 28, 2011 at 19:57 UTC
    ... no matter how I reference that [hash] variable ..., [it] always begins at "1".

    From the behavior you describe, I strongly suspect you were not running with strictures and warnings enabled; had they been, you would have received some useful hints. In the first example below, run without strictures and warnings, the scalar variable  $hash is created and initialized, but there is no  %hash hash variable created explicitly; instead, it is autovivified.

    >perl -le "my $pbxex = 42; my $hash = $pbxex; ;; for (qw(CS EE EE CS EE)) { $hash{$_}++; printf qq{'$_$hash{$_}' }; } " 'CS1' 'EE1' 'EE2' 'CS2' 'EE3' >perl -wMstrict -le "my $pbxex = 42; my $hash = $pbxex; ;; for (qw(CS EE EE CS EE)) { $hash{$_}++; printf qq{'$_$hash{$_}' }; } " Global symbol "%hash" requires explicit package name at -e line 1. Global symbol "%hash" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

    If I correctly understand what you are trying to do, I would think that something like toolic's approach would be best.