Home > Enterprise >  Perl : How to sort value and put them into array
Perl : How to sort value and put them into array

Time:05-25

I'm very new to Perl but here's what I'm trying to do for example the variable has a value of apple,banana,cat,dog, I need to put this value into individual array like for example

my $value = "apple","banana","cat","dog";

Result:

my @A = "apple";
my @B = "banana";
my @c = "cat";
my @D = "dog";

just like that? I hope someone help me on this one , thank you very much

CodePudding user response:

because I need it to be arranged in alphabetical order using array as a holder for those value. so if the script capture this letter it will go to that array , for example if the script has the value of yoyo it will go to array @Y. just like that , I hope you understand what I mean. thank you

It looks like what you want to do is create a multi-level data structure, so you can report something like this.

A: apple, astronaut
B: banana, buttons
C: cat, carnage
D: dog
P: perl
Y: yoyo, yawn

Now you would not typically use 26 different arrays for that. That's really bad style, and also incredibly hard to maintain, as you can't make it dynamic.

Instead, you would use a multi-level data structure. A hash reference is great for that.

$sorted_words_by_first_letter = {
  'a' => [ 'apple', 'astronaut' ],
  'b' => [ 'banana', 'buttons' ],
   # ...
}

You would have to iterate the list, look at the first letter of each word, and put them into the right key inside your hash reference.

my $sorted_words_by_first_letter;
my @words = qw/ banana apple astronaut yoyo buttons /;
foreach my $word (@words) {
  
  # find the first character
  $word =~ m/^(.)/; 
  
  # put it at the end of the array for the appropriate letter
  push @{ $sorted_words_by_first_letter->{$1} }, $word;
}

You can read more about references and multi-level data structures in Perl at perlref and perlreftut. You can read more about regular expressions at perlre.

If your input is not a list, but a string, you need to split it first.

my @words = split /,/, "banana,apple,astronaut,yoyo,buttons";

To turn that back into the output I showed above, you need to iterate the keys.

foreach my $letter ( sort keys %{ $sorted_words_by_first_letter } ) {
  printf "%s: %s\n", uc($letter),
    join ', ', sort @{ $sorted_words_by_first_letter->{$letter };
}

That'll output the following:

A: apple, astronaut
B: banana, buttons
Y: yoyo

To show you why you shouldn't use specific variables for each different letter, consider this code. I'm only showing this so you see why it is a bad idea. Please don't do this.


my ( @A, @B, @C, @D, @E, @F, @G, @H, @I, @J, @K, @L, @M, 
     @N, @O, @P, @Q, @R, @S, @T, @U, @V, @W, @X, @Y, @Z );

my @words = qw/ banana apple astronaut yoyo buttons /;
foreach my $word (@words) {

  # find the first character, assign to var
  ( my $letter ) = $word =~ m/^(.)/; 

  if ($letter =~ m/a/i) {
    push @A, $letter;
  } elsif ($letter =~ m/b/i) {
    push @B, $letter;
  } elsif ($letter =~ m/c/i) {
    push @C, $letter;
  } # ...

As you can see, this gets very long and unwieldy very quickly, and we haven't even talked about how to use it afterwards. That would require the same amount of repetition again. So please don't do this. All it does is make your code really hard to maintain.

CodePudding user response:

I think you're misunderstanding some concepts.

Variables that start with a dollar sign $ can only hold a single value. They are called scalars, and they cannot be arrays.

my $value = "apple","banana","cat","dog";

The value of $value will be just be "apple". The other parts of that list will be discarded.

Your variables @A, @B, @C and @D are all arrays. When a variable has an at @ as its sigil, it's an array.

my @A = "apple";
my @B = "banana";
my @c = "cat";
my @D = "dog";

This code indeed creates four arrays with one value each, which is fine. But it's probably not what you were asked to do.

I think what you meant to do was assign the list of words into an array, and then assign each element of the array to an individual scalar variable.


# make an array of words
my @words = ( "apple","banana","cat","dog" );

# assign each one to a single variable.
my ($apple, $banana, $cat, $dog) = @words;

The above code uses list assignment with a list of variables on the left hand side of the equals = to put the elements of the array into those variables.

Another way of doing this is to assign each one individually by accessing elements in the array. To do this, the sigil changes from an @ to a $.

my $apple  = $words[0];
my $banana = $words[1];
my $cat    = $words[2];
my $dog    = $words[3];

None of this has anything to do with sorting, though.

If you want to sort the array, you'd use the sort built-in. Your list is already sorted alphabetically, so this is not very exciting.

my @sorted = sort @values;
print "@sorted"; # apple banana cat dog
  •  Tags:  
  • perl
  • Related