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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Perl! array/file access question

  • 03-04-2000 3:03pm
    #1
    Closed Accounts Posts: 577 ✭✭✭


    Lo, learning a bit of Perl at the moment and I'm having problems accessing the elements in an array.

    This is what I want to do. Read and store all the lines of a file into an array (this is easy). Save a sentence stored in the array into a string, split the string into an array of words and then access each element. The code looks like this...


    #!/usr/local/bin/perl
    open (FILE1, "file1.txt");
    @list = <FILE1>;
    close (FILE1);

    @subarray = split (/ /, $list[0]);
    print ("Start...\n");
    print ("@subarray\n&quot;);
    print ("Element 0 = $subarray[0]\nElement 1 = $subarray[1]");
    print ("End.\n");

    ****file1.txt****
    Just a sample sentence in this file.


    Output I get when I run the script is....

    Start...
    Justasamplesentenceinthisfile.
    Element 0 =
    Element 1 =
    End.


    The split seems to have gone ahead but when I access the subarray elements it's blank. This doesn't happen if the sentences were read from standard inputline instead of taking from an array from a file. Anyone know why this happens and how to fix it?



    [This message has been edited by Chubby (edited 03-04-2000).]


Comments

  • Moderators, Social & Fun Moderators Posts: 28,633 Mod ✭✭✭✭Shiminay


    Originally posted by Chubby:

    @subarray = split (/ /, $list[0]);

    You're adding all the stuff into one array element. You'll want to do a while loop and assign the values into each element one at a time.



    All the best,

    Dav
    @B^)
    http://homepage.eircom.net/~davitt


  • Closed Accounts Posts: 577 ✭✭✭Chubby


    Originally posted by Kharn:
    You're adding all the stuff into one array element. You'll want to do a while loop and assign the values into each element one at a time.

    Hmm, I think you are right but this works if the line I am splitting is not part of an array.

    The codes below for example works perfectly. It adds all the words from the split function into seperate array elements. This is some bloody odd behaviour.

    $line = <STDIN>;
    @subarray = split(/ /, $line);
    print ("$subarray[0],$subarray[1],$subarray [2] etc");



  • Closed Accounts Posts: 577 ✭✭✭Chubby


    That still doesn't let me do what I want to do. The original code should work but it's putting everything into the last accessed element of the subarray! Any ideas how to do this?


  • Moderators, Social & Fun Moderators Posts: 28,633 Mod ✭✭✭✭Shiminay


    Are you sure you've got the split function right? Is it not:
    @subarray = split ' ', $line;

    This will split by spaces I think. I've see the // you have in your example in places in the book (The Camel Book) I'm using, so maybe both are acceptable.

    Thing is, I'm as new to this language as you seem to be smile.gif I am however a programmer of other languages and code is code wink.gif

    Let me know how you get on.



    All the best,

    Dav
    @B^)
    http://homepage.eircom.net/~davitt


  • Closed Accounts Posts: 577 ✭✭✭Chubby


    Originally posted by Kharn:
    Are you sure you've got the split function right? Is it not:
    @subarray = split ' ', $line;

    This will split by spaces I think. I've see the // you have in your example in places in the book (The Camel Book) I'm using, so maybe both are acceptable.

    Thing is, I'm as new to this language as you seem to be smile.gif I am however a programmer of other languages and code is code wink.gif

    Let me know how you get on.

    Yeah I found out what I was doing wrong. The lines in the txt file all have multiple spaces between the words. The split function was only looking for 1 space.

    The correct code should be
    @subarray = split (/ +/, $list[0]);
    The + sign makes it grab as many spaces as it can etc. You confused me with the loop suggestion wink.gif but thanks for the help.


  • Advertisement
  • Moderators, Social & Fun Moderators Posts: 28,633 Mod ✭✭✭✭Shiminay


    Ah, g'man. I've only been reading the books - not delved into any "hello world"'s yet (been too busy!)

    Good too see it's sorted smile.gif



    All the best,

    Dav
    @B^)
    http://homepage.eircom.net/~davitt


  • Closed Accounts Posts: 6,601 ✭✭✭Kali


    with the split function if you dont specify the pattern match // then it uses white space... so theres never any need to use the ' ' or " ".

    quickest way of printing out each element along with its position in the array:

    $c = 0;
    foreach (@a) {print "Element: $c "; print; $c++}


  • Moderators, Social & Fun Moderators Posts: 28,633 Mod ✭✭✭✭Shiminay


    Now why didn'tyou just post that a couple of days ago??? wink.gif

    Cheers



    All the best,

    Dav
    @B^)
    http://homepage.eircom.net/~davitt


Advertisement