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

I am trying to integrate, AuthorizeNET. Their RESPONSE is a string, with 39 fields, seperated by a comma.

Is there an easier way to seperate the response, then by doing something like this:
($_r1,$_r2,$_r3,$_r4,$_r5,$_r6...) = split /\,/ $response, 39;

I thought about doing a while statement, with a counter, so I know which number it is.

Anyways, I know there MUST be a simpler way. Do you know of one?

Thanks,
Richard

Replies are listed 'Best First'.
Re: split questions
by leriksen (Curate) on Jun 30, 2003 at 04:23 UTC
    I wouldn't try to store all the fields in individual scalars, but store them in an array like
    @fields = split /,/, $response, 39;
    Also, on a style-note, calling the fields $_r1, $_r2 etc doesn't really convey a lot of information about what the fields are (then again, maybe your real code does use names like
    ($date,$time,$type...) = ...
    )
    You can use the following to name the fields in the array
    use constant DATE => 0; use constant TIME => 1; use constant TYPE => 2; ... my @fields = split /,/, $response, 39; if ($fields[TYPE] eq 'widget') ...
    You could also use a hash -
    my @fields = qw(date time type ...); my %response; @response{@fields} = split /,/, $response, 39; # hash slice if ($response{type} eq 'widget') {...
Re: split questions
by Zaxo (Archbishop) on Jun 30, 2003 at 04:17 UTC

    Your split should be fine so long as there is no extraneous whitespace. Your assignment would be better as my @response = split /\,/ $response, 39; or

    my %response; @response{qw/names for all the fields/} = split /\,/ $response, 39;
    depending on whether you want mnemonic identifiers for the fields.

    After Compline,
    Zaxo

Re: split questions
by revdiablo (Prior) on Jun 30, 2003 at 04:14 UTC

    Using an array comes to mind:

    my @response = split /\,/, $response, 39;
Re: split questions
by BrowserUk (Patriarch) on Jun 30, 2003 at 06:13 UTC

    Like everyone else I'd advise an array rather than individually names scalars. I add the twist of using constants to give the individual elements of that array meaningful names.

    use constant CODE=>0; use constant SUBCODE=>1; ...

    If your using 5.8 then your can combine the constant declarations into one

    use constant { CODE=>0, SUBCODE=>1, .... };

    Your code can then read something like this

    if( $response[CODE] == 20 and $response[SUBCODE] == 55 ) { # Do something appropriate }

    Which is nicely self-documenting and efficient to boot.

    If your application lends itself to it, then the above test could become

    if( "@response[CODE, SUBCODE]" eq '20 55' ) { # do stuff }

    which can greatly simplify and clarify coumpound conditionals provided the individual elements don't contain spaces.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: split questions
by davorg (Chancellor) on Jun 30, 2003 at 08:34 UTC

    If I can add one more suggestion to the mix, it looks to me that a hash would be the best way to store your data. And you can populate a hash in one step using a hash slice.

    my @fields = qw(code subcode ...); # List of fieldnames my %response; @response{@fields} = split /,/, $response, 39;

    You can then access individual fields from the response using syntax like $response{code}, $response{subcode}, etc.

    Of course, depending on how much you intend to reuse this piece of code, it might be worth going the whole way and creating a Response object.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: split questions
by jsprat (Curate) on Jun 30, 2003 at 04:34 UTC
    Everyone above responded "use an array", and that's good advice.

    I'd like to go one step further and say that any time you have sequentially numbered variables (like $_r1, $_r2), think about using an array. Using an array will make the code more readable and more maintainable.

    HTH...

Re: split questions
by powerhouse (Friar) on Jun 30, 2003 at 05:00 UTC
    Thank you everyone!!

    I am not really using $_r1, it was just for the example, the names are more like this:
    ($Response_Code,$Response_Subcode) #... etc. Pretty long names.

    I'll try the array, then they will be like $array_name[0] that is the first "variable" $Response_Code right? then 1 is the second spot, 2 the third, and so forth?

    Thank you everyone! I appreciate the advice!
    thx,
    Richard