I really don't think you want to use hashes here.  A hash is a one-way
association.  In other words, I have a person's name and I want to look up
his address or I have a phone number and I want to look up who lives there.
What you are asking for is a way to store two objects together.  One really
doesn't identify the other, they just both want to live happily together.
For this, you should use an array reference.  Array references are declared
by the left and right bracket [].  So, for the example you gave you would
say
my $aref = [$pdf1, $link1];

Then, you can access $pdf1 by using $aref->[0].  Notice that this is the
same syntax for regular arrays, but the dereferencing operator -> has been
used between the variable and the index.  Also notice that the variable is a
scalar, not an array.  References are always scalars.  Since you have a
bunch of pdfs that need to be linked with link, you can put the array
refereces into an array.

my @arr = ([$pdf1,$link1], [$pdf2,$link2], [$pdf3,$link3]);

or if you are reading them in from the user
while( my ($pdf,$link) = get_input() ) {
  push @arr, [$pdf, $link];
}

Then, you can do your foreach loop

foreach my $pair ( @arr ) {
  if( $pair->[0] && $pair->[1] ) {
    exec( "ln -s $pair->[0] $base/$pair->[1]" );
  }
}

HTH,
Tanton
----- Original Message -----
From: "David vd Geer Inhuur tbv IPlib" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 01, 2002 3:53 AM
Subject: How to fill and use Hashes


>
> Hi There,
>
> I have never used hashes before, althought they seem to be interesting I
didn't
> know the exact use for it. But as times are changing I think I just came
to a point
> where I need a hash, but how do I create one and how do I use it ?
>
> Can anyone assist ? this is what I want to accomplish :
>
> foreach(HASH) {
>   if (($pdf1) && ($link1)) {
>     exec("ln -s ${link1} ${base}/${pdf1}");
>   } # End if
> } # End foreach
>
> So I have 2 variables that always come together. To write an if then
statement
> for 10 times because I have 10 * pdf and link variables looks bad to me.
> So I am looking for the foreach HASH something. I know it must be
possible.
> Who can assist ?
> Plus, how do I fill a HASH ?? : " HASH = ($pdf1, $link1); "?? How to
proceed
> with my next " HASH[1] = ($pdf2, $link2); " ??
>
> Thanks for your help in advance !!!
>
> Regs David
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to