ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Okay. I've Been tinkering with this code. My goal is to reverse the elements of an array, without using the reverse operator of course.
#!/usr/bin/perl
my @array = (1,2,3);
sub reverse_array {
my $arr_ref = shift;
my $rev_array = [];
foreach my $item (@$arr_ref) {
if (arr_ref($item) eq @array) {
$item = reverse_array($item);
}
unshift @$rev_array, $item;
}
return $rev_array;
}
reverse_array(@array);
print @array;
I am new to perl, so I pasted this together from various sources. A few things I dont understand if anyone could clarify, what does eq and arr_ref function as?
particularly, I don't understand this line of the code
if (arr_ref($item) eq @array)
Also, this code doesn't run successfully. Thanks in advance, Ryan.
To answer your questions about the code you posted -- eq is like == (sort of). arr_ref isn't a function, it's a mistyped array name and index (should be $arr_ref[$item]). The line if (arr_ref($item) eq @array) is checking to see if $arr_ref[$item] is an array, and if it is, recuse into reverse_array with it.
I have another perl question, but I don't want to flood the board with my perl cries so ill just pop it on here.
I'm trying to open a file with a standard format like
/x/y/z
then I want to read all the lines in the file and print an output of columns such that
X Y Z
So how could i scan a line and assign each category to an element in an array ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.