?
Solved

Syntax Question

Posted on 2004-12-01
7
Medium Priority
?
227 Views
Last Modified: 2010-03-05
What is the meaning of the line below:

     my $hh=join('+',sort {$a <=> $b} @l) ; $f[$t]=$hh ;

Also could you explain :

  foreach my $m (split(/\+/,$f[$t])) {
              push @cl,$mbd[$m] ;
           }
0
Comment
Question by:Cosine_Consultants
7 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 12717346
have you considered reading the documentation that comes with Perl?
0
 
LVL 13

Expert Comment

by:gripe
ID: 12717488
What algorithm was the file encrypted with?

0
 
LVL 13

Expert Comment

by:gripe
ID: 12717503
Argh, damn quickpost. :)

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
sstouk earned 2000 total points
ID: 12717976
This portion:

my $hh=join('+',sort {$a <=> $b} @l) ;  $f[$t]=$hh ;


Array @l contains elements that are sorted from lowest to highest value and joined with the '+' sign
The result is a scalar, assigned to variable $hh, which is in turn assigned to the element $t or the array @f;

For example,
let's say
@l = ('222','333','111');
After:
$hh=join('+',sort {$a <=> $b} @l) ;  

$hh will be equal to  '1+2+3'
and if We have an array with elements
@f = ('first','second','third');
and $t = 1, then after
$f[$t]=$hh ;

$f[$t] will be '1+2+3';
And
@f will be equal to ('first','1+2+3','third');

##########################################################
The Next process takes the value
'1+2+3' and splits it in places where the character '+' is into elements.
It loops though the elements, assigning each current value to variable $m

foreach my $m (split(/\+/,$f[$t]))
{
push @cl,$mbd[$m] ;
}

For each iteration, variable $m will take a value of  '1', then '2', then '3' because we split the Scalar into these elements.

There is an array @mbd with some elements in it;
If For Example
@mbd = ('First','Second','Third','Fourth');

Then element $mbd[1] is "Second";
Element $mbd[2] is "Thurd"; etc.

So when we loop though the splitted elements of the scalar variable $f[$t] (which is just one element of an array @f) we get
$mbd[1] then $mbd[2]  then $mbd[3]  because $m = 1, then $m = 2, then $m = 3
Command 'push'; pushes the values of the array $mbd into a new array - @cl

After the loop finishes, @cl = ('Second','Third','Fourth');

###################################################################

There is nothing more to it.





0
 
LVL 6

Expert Comment

by:sstouk
ID: 12718109
Change
#################
For example,
let's say
@l = ('222','333','111');
#################
To:
#################
For example,
let's say
@l = ('2','3','1');
#################
0
 
LVL 5

Expert Comment

by:roee_f
ID: 12718138
a. as some has pointed, you're gonna have to buy a good Perl book (I recommend on "Learning Perl" if you're new to programming and or perl. Also, "Programming Perl". Both of them are 3rd edtioins from O'reilly.

b. Here is a sample code for your first question:
my @l = (4,2,8,4,10);
my $hh=join('+',sort {$a <=> $b} @l) ;
print "$hh\n";

sort {$a <=> $b} @l

sort function gets as parameters
a. sorting function (which defaults to asciibetically)
b. a list to sort
Here the sorting function is to sort numerically. This is the meaning of the "Spaceship" operator (<=>). $a and $b are special variables to sort, which tells it, in that case to sort in ascending order.

join gets as parameters
a. a joiner character
b. a list to join.
It returns as result the string.

Run this code to see what I mean.

$f[$t]=$hh ;

This is a simple assignment of the result to the $t element in @f array.

c.
foreach my $m (split(/\+/,$f[$t])) {
              push @cl,$mbd[$m] ;
           }

split(/\+/,$f[$t])

split is the reverse of join.
It gets
a. the character to split according to
b. the string to split
It returns a list.

This loop iterates on each element of this list, (which seems like a list of array's indices) in each iteration pushing the result to @cl array.
0
 
LVL 85

Expert Comment

by:ozo
ID: 12723840
c. could also be
push @cl,@mbd[split(/\+/,$f[$t]];
or, combining the two:
push @cl,@mbd[sort {$a <=> $b} @l];

see
perldoc -f sort
perldoc -f join
perldoc -f split
perldoc -f push
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question