Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl Array element manipulation

Options
  • 05-01-2006 8:20pm
    #1
    Registered Users Posts: 7,859 ✭✭✭


    hey, I've looked around online for examples but cant get any. i was wondering if any one can help me with this, what seems to be easy, problem?

    basically, i am reading in a sentence like "Hello World" but i need to be able to deal wit each individual letter separately.

    i tried reading it into an array but had trouble with that for some reason. then tried reading into a variable then putting the variable into an array but the element 1 was "Hello world" as opposed to "H".

    Is it possible to read directly into an array in perl from STDIN? or are there other ways to read in letters and spaces seperately? can chomp or any of them functions be used?

    sorry for all the questions, but i dont have much experience in perl!

    TIA.


Comments

  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    Do you really need an array?
    Would looping through the string read from stdin and using substr to select each character in turn do the job?


  • Registered Users Posts: 7,859 ✭✭✭The_B_Man


    coz for each letter of the alphabet, i have another arrray filled wit corresponding values that they need to be converted to. though, now that i look at it, i suppose it could be done that way to. I never used that substr before so ill give it a go and let yis know. fell free to post any more ideas.

    also, i was considering a hash thing for converting the alphabet. maybe theres an easier way doing it that way?


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    Is this like a substitution cipher where every character read in is replaced by one in the substitution array?

    In any event the split function will convert your input string into an array.


  • Registered Users Posts: 7,859 ✭✭✭The_B_Man


    well I used the substr function like u said so im able to extract every character using a while loop.

    I now hav a hash table (or watever its called) wit values such as 'A' => 'x1', 'B' => 'x2' etc etc.

    wat i need to do now is convert, say, "ABBA" to "x1 x2 x2 x1" and vice versa. i'm looking over hashes now and it seems relatively straight forward. hopefully it is! haha


  • Closed Accounts Posts: 304 ✭✭Zaltais


    The following is the easiest way to seperate every character into an array.
    (The 'use Data:: Dumper;' and 'print Dumper(\@array);' lines are purely to ilustrate the array...)
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $value = "Hello World\n";
    
    chomp $value;
    
    my @array = split(//, $value);
    
    print Dumper(\@array);
    

    I'll leave you to figure hashes out for yourself, but post back if you've any problems.


  • Advertisement
  • Registered Users Posts: 2,593 ✭✭✭tommycahir


    I ahve found the answer to most of my perl questions can be answered hereperl monks. they have quiet an extensive thread history and a simple search will bring up an answer to most questions

    Hope it helps anyway


  • Registered Users Posts: 7,859 ✭✭✭The_B_Man


    thanks ill try perl monks.

    at the moment i actually have it working one way. i can convert sentences, but the problem is converting it back the other way.

    whats the perl code for saying:

    if its alphabetic then do this

    else do that

    i tihnk its like

    if [a-z][A-Z]
    {
    etc...
    }
    else
    {
    bleh...
    }

    am i on the right track?


  • Registered Users Posts: 2,593 ✭✭✭tommycahir


    if you put the code that you have done and let us have a look at it then we might be able to help you a little bit more that just giving you links etc.


Advertisement