Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

avoiding the hash

by Anonymous Monk
on May 09, 2003 at 09:57 UTC ( [id://256806]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, I wondered if there was a way of pairing up the numbers from two arrays (e.g. $array[[0]] with $array2[[0]] etc for the whole array of unknown size) without using a hash. The reason is that I need to keep my data in the same order as the arrays hold it. Cheers.

Replies are listed 'Best First'.
Re: avoiding the hash
by smitz (Chaplain) on May 09, 2003 at 10:09 UTC
    Yuk:
    for (0 .. scalar(@array1)) { $temparray[$_] = $array[$_] . '|' . $array2[$_]; }
    then you can split @temparray on the bar (|)

    Smitz
    this reply makes me feel dirty
      will fail as soon as you have '|' in any of array's elements.
        He says numbers. The first time I too thought he needs an escape function.
        I wondered if there was a way of pairing up the numbers from two arrays

        I assumed numbers in this case matched \d

        Smitz

        Update Messed up my Regex, thanks nkuvu
      perfect - thankyou!
Re: avoiding the hash
by Skeeve (Parson) on May 09, 2003 at 10:24 UTC
    You simply want to avoid it because of the order? So why not not-avoid and do it this way:
    @hash{@array}=@array2; foreach $v (@array) { print $hash{$v}; }
    You have paired them in %hash and keep the sequence in @array.

      Which is OK as long as @array dosen't contain duplicates ...

      perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
        *sigh* There is always a downside :-/
Re: avoiding the hash
by zby (Vicar) on May 09, 2003 at 10:03 UTC
    You can put them into a string:
    $array[0] . "|" . $array2[0]

    Or you can put them into an array:

    my $newarray[0] = [$array[0], $array2[0]];

    Update: Got rid of the escape function: they are just numbers. Got rid of the temporary variable.

Re: avoiding the hash
by bart (Canon) on May 09, 2003 at 12:08 UTC
    You want a hash, but one that maintains the order? Just like in PHP? Then perhaps look into one of those many hash modules on CPAN. The FAQ only mentions Tie::IxHash, and seen its age, I gather it must be one of the more robust modules for this purpose.
Re: avoiding the hash
by Anonymous Monk on May 09, 2003 at 12:26 UTC
    Your playing with fire, this an intrinsicly dangerous thing, mainly because no concrete coupling will exist between the two separate arrays and so your data could easily get out of sequence. I think what you are looking for is : 1) An Array of Arrays (perls way of doing higher dimentional data structures) 2) A routine to take your pair of arrays and combine them (re-index) into the array of arrays.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-07-25 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.