You must be logged in to post Login Register

Search Forums:


 






Perl

UserPost

9:22 am
April 20, 2010


lost_prophet_

Member

posts 12

Post edited 9:47 am – April 20, 2010 by lost_prophet_



Ok, another one from me haha, this time i need help sorting lines of text.

I have a file full of lines like this:


("ADD_SCORE", playerIndex, score)
("ALLOW_PLAYER_TO_CARRY_NON_MISSION_OBJECTS", playerIndex, allow)
("ALTER_WANTED_LEVEL", playerIndex, level)
("ALTER_WANTED_LEVEL_NO_DROP", playerIndex, level)
("APPLY_WANTED_LEVEL_CHANGE_NOW", playerIndex)
("CHANGE_PLAYER_MODEL", playerIndex, model)
("CLEAR_PLAYER_HAS_DAMAGED_AT_LEAST_ONE_PED", playerIndex)
("CONVERT_INT_TO_PLAYERINDEX", playerId)
("CLEAR_WANTED_LEVEL", playerIndex)
("CREATE_PLAYER", playerId, x, y, z, pPlayerIndex)

And so on… it's alot of functions for use in scripting plugins for GTA IV.


I want to sort the lines into the function name, and how many parameters it has (and possibly the parameters too, even though they are there, this way would be neater IMO).

E.G.

("ADD_SCORE", playerIndex, score)

Would turn into:

Function = ADD_SCORE, Parameters = 2 (playerIndex, score)

(or something like that).


The way i've decided to count the parameters is to count the commas :P , so like:


#!/usr/bin/perl

use strict;
use warnings;

print "File: ";
chop(my $file = <STDIN>);

open(FILE, $file);
my @lines = <FILE>;
close(FILE);

print "Save file: ";
chop(my $savefile = <STDIN>);

open(SAVE, ">$savefile");

foreach (@lines) {
    my $count = ($_ =~ tr/,//);
    chop($_);
    print SAVE "The function: $_, contains $count parameters.n";
}

close(SAVE);


This returns: "The function: ("ADD_SCORE", playerIndex, score), contains 2 parameters." (for the first line).

I would like it to return: "The function: "ADD_SCORE", contains 2 parameters (playerIndex, score)." (i wouldn't mind if the function was in talking marks or not).

I've tried using the split() function, but that doesnt work too well.


9:58 am
April 20, 2010


Teddy

EU

Elite Member

posts 267

I would use split as well. But perl is not my first language Frown. May u can post the code where u use split: I would use something like that:

foreach (@lines) {
    my $count = ($_ =~ tr/,//);
    chop($_);

   @parts =split(/ /); //After every space try a new position in array
    print SAVE "The function: $_, contains $count parameters( $parts[0], $parts[1]).n";
}


With great power comes great responsibility. Have a look at this webpage: securityoverride.com

8:45 pm
April 20, 2010


lost_prophet_

Member

posts 12

Post edited 10:34 pm – April 20, 2010 by lost_prophet_



I've tried a few more things, so far i've got it to do this:

The function: ("REGISTER_PLAYER_RESPAWN_COORDS", playerIndex, x, y, z), contains 4 parameters – (playerIndex, x, y, z).
The function: ("SET_EVERYONE_IGNORE_PLAYER", playerIndex, value), contains 2 parameters – (playerIndex, value).
The function: ("SET_PLAYER_CAN_BE_HASSLED_BY_GANGS", playerIndex, value), contains 2 parameters – (playerIndex, value).
The function: ("SET_PLAYER_CAN_DO_DRIVE_BY", playerIndex, value), contains 2 parameters – (playerIndex, value).
The function: ("SET_PLAYER_CAN_USE_COVER", playerIndex, value), contains 2 parameters – (playerIndex, value).
The function: ("SET_PLAYER_CONTROL", playerIndex, value), contains 2 parameters – (playerIndex, value).
The function: ("SET_PLAYER_CONTROL_ADVANCED", playerIndex, unknown1, unknown2, unknown3), contains 4 parameters – (playerIndex, unknown1, unknown2, unknown3).

Which is exactly what i want (for the end of the line at least).


The code I came up with is very sloppy (Thanks to Teddy for the idea), could be changed and probably shortened, but i want to get the script doing what i want before i try to optimize it.

foreach my $function (@lines) {
    my $count = ($function =~ tr/,//);
    chop($function);
    print SAVE "The function: $function, contains $count parameters";
    if ($count > 0){
        print SAVE " – (";
    }
    my @parts = split(/ /, $function);
    my $partssize = @parts;
    foreach(my $i = 1; $i < $partssize; $i++){
        if ($i == 1) {
            print SAVE "$parts[$i]";
        }
        else {
            print SAVE " $parts[$i]";
        }
    }
    print SAVE ".n";
}

(that's the only bit I changed).


Right now im working on how to separate the function name from $function using substr(), but substr() doesn't like me very much :p, e.g. –

The function: ("REGISTER_PLAYER_RESPAWN_COORDS", playerIndex, x, y, z), contains 4 parameters – (playerIndex, x, y, z).

I've got it to return this –


The function is: "ADD_SCORE", playerIndex, score).

Using the following code:


foreach my $function (@lines) {
    my $count = ($function =~ tr/,//);
    chop($function);
    my $this1 = ($function =~ /"/g); # this get the first quotation mark, i cant get the last one, i cant figure out how to.
    my $function2 = substr($function, $this1); #, $this2);
    print SAVE "The function is: $function2.n";
}

 

 Edit:

This code works pretty much as i intended it to. Still a bit sloppy, but it works. Could anyone maybe try and optimize this/make it smaller?


 #!/usr/bin/perl

use strict;
use warnings;

print "File: ";
chop(my $file = <STDIN>);

open(FILE, $file);
my @lines = <FILE>;
close(FILE);

print "Save file: ";
chop(my $savefile = <STDIN>);

open(SAVE, ">$savefile");

foreach my $function (@lines) {
    my $count = ($function =~ tr/,//);
    chop($function);
    my $index = index($function, "\""); # Gets the first quotation mark.
    my $index2 = index($function, "\"", $index + 1); # Gets the second quotation mark.
    my $function2 = substr($function, $index, $index2);
    if ($count == 1) {
        print SAVE "The function: $function2 contains $count parameter";
    }
    else {
        print SAVE "The function: $function2 contains $count parameters";
    }
    if ($count > 0){
        print SAVE " – (";
    }
    my @parts = split(/ /, $function);
    my $partssize = @parts;
    foreach(my $i = 1; $i < $partssize; $i++){
        if ($i == 1) {
            print SAVE "$parts[$i]";
        }
        else {
            print SAVE " $parts[$i]";
        }
    }
    print SAVE ".\n";
}

close(SAVE);



About the Infinity Exists forum

Most Users Ever Online:

164


Currently Online:

13 Guests

Forum Stats:

Groups: 4

Forums: 22

Topics: 1962

Posts: 9690

Membership:

There are 8181 Members

There has been 1 Guest

There are 2 Admins

There are 3 Moderators

Top Posters:

GONZO – 569

slicer45 – 270

Teddy – 267

madf0x – 229

clarke – 223

gube – 214

Administrators: Patchy (1645 Posts), Nox (40 Posts)

Moderators: CrashOverron (377 Posts), Override (207 Posts), Copy (163 Posts)