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

Say you had a list of user ids:
my ($temp) = 121, 122, 123, 124, 125, etc...
If the size of the list is always changing, how would you go about splitting it to retrieve all the id's individually?

Replies are listed 'Best First'.
Re: Splitting a dynamic comma-separated list.
by virtualsue (Vicar) on Jul 26, 2002 at 15:49 UTC
    my ($temp) = 121, 122, 123, 124, 125, etc...
    my $temp = "121, 122, 123, 124, 125, 126"; my @userids = split /\s*,\s*/, $temp;
    This will split your scalar containing a comma-separated list of values into an array containing each of those values. The array will automagically have as many elements as necessary.
Re: Splitting a dynamic comma-separated list.
by aersoy (Scribe) on Jul 26, 2002 at 14:58 UTC

    Hello,

    I guess that list is a string. Therefore using split will suffice.

    my ($temp) = "121, 122, 123, 124, 125, etc."; foreach my $uid (split /[[:space:]]*,[[:space:]]*/, $temp) { print $uid, "\n"; }

    perldoc -f split is what you should be reading. I hope this helps.

    --
    Alper Ersoy

Re: Splitting a dynamic comma-separated list.
by BrowserUk (Patriarch) on Jul 26, 2002 at 19:35 UTC

    my ($temp) = 121, 122, 123, 124, 125, 126;

    If you are already using that line in a program,literally (pun intended:), and you are not receiving warnings about it when you run your program, you should add -w to the shebang line (ie.#!/usr/bin/perl -w) or the command line when you run it; or add use warnings; at the top of your program.

    If you had done this, you would received warnings something like:

    Useless use of a constant in void context at yourscript.pl line 7. Useless use of a constant in void context at yourscript.pl line 7. Useless use of a constant in void context at yourscript.pl line 7. Useless use of a constant in void context at yourscript.pl line 7. Useless use of a constant in void context at yourscript.pl line 7.

    What this would have told you is that all but the very first of your values is being thrown away. So, in your example, the scalar $temp is being given the value 121, and everything else is being discarded.

    The methods shown by other monks above all work, but maybe they forgot the simplest, which would be to assign the values to an array, and then use them, as follows:

    my @userids = ( 121,122,123,124,125,126 ); #etc... if ($userid[0] == 121) { do something... } if ($userid[1] == 122) { do somthing else... }

    Note: When refering to individual elements of the @userids array, each of which is a scalar, we use the $ prefix character (sigil). and also that Perl arrays start at index zero, [0].

    Finally, if your list of user ids keeps changing, rather than editing the script, you would be better putting them in a seperate file something like this.

    #! perl use warnings; my $useridfile = 'userids'; open USERIDS, $useridfile or die "Couldn't open $useridfile; reason $! +"; my (@userids) = (<USERIDS>); close USERIDS or warn "Couldn't close userid file:$!"; for my $count (0 .. (scalar @userids) - 1) { print "userid[$count]=", $userids[$count]; } __END__ #if the file "./userids" contains: 121 122 123 124 125 126 #the above program would give C:\test>185550 userid[0]=121 userid[1]=122 userid[2]=123 userid[3]=124 userid[4]=125 userid[5]=126 userid[6]=127
Re: Splitting a dynamic comma-separated list.
by hiseldl (Priest) on Jul 26, 2002 at 17:31 UTC
    If your list is a scalar then the previous replies will work excellently, if you mean 'list' as an array reference, i.e.: my ($temp) = [121, 122, 123, 124, 125, etc...] you could use
    foreach $id (@$temp) { # do things with $id }
    or if you wanted a different way, you could assign it to a hash:
    @hash{@$temp}=@$t; foreach $id (keys %hash) { # do things with $id }
    with this method you can use the hash to determine if your id exists using the id itself, e.g.
    if ($hash{120}) { # id 120 exists }
    TMTOWTDI!

    --
    hiseldl