Sponsored Content
Top Forums Shell Programming and Scripting perl - how can we name a variable base on value of another variable Post 302359533 by daptal on Tuesday 6th of October 2009 11:08:21 PM
Old 10-07-2009
Code:
perl -e 'my $x= 100;
> ${x.$x} = 2000;
> print "$x \n";
> print "$x100 \n";
> '
100
2000

Note: This is not a standard method of doing. When requirement like these arises it is preferable to use hashes

Cheers

Last edited by pludi; 10-07-2009 at 03:16 AM.. Reason: code tags, please
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable to perl

I need a non-perl (bash) way to strip the path from a list of "find" results. Below is the perl version which I could use, if I could figure out how to call the script with a variable (like in sh, $1 is the variable passed in ./script variable) $file = "/path/to/file.txt"; # How do I... (2 Replies)
Discussion started by: TheCrunge
2 Replies

2. Shell Programming and Scripting

Multiple variable in a variable in Perl

Hi All, I am trying to convert the below Csh while loop into Perl while loop but the problem is that in this csh script, i have 2 variables inside a variable -> $count is a variable {SB$count} as a whole is another variable. Csh is able to assign values to such variable like the below but i do... (3 Replies)
Discussion started by: Raynon
3 Replies

3. UNIX for Dummies Questions & Answers

variable created base on other var value

hi, i'm trying to create a script in ksh that will check build logs for errors. i'm using a variable to hold the grep, somthing like: grepErr=`echo "grep -i -e ' error ' -e ' errors ' -e 'rror(s)' -e 'Unsatisfied ' -e 'Undefined symbol' -e 'No rule ' -e 'Cannot' -e 'rror:' -e 'Could not find... (2 Replies)
Discussion started by: lavinia_f
2 Replies

4. Shell Programming and Scripting

perl get variable value ???

hi i have following code my $a1 = "A" ; my $a2 = "B" ; my $a3 = "C" ; foreach my $k ( 1,2,3 ) { my $msg = ${a{$k}} # this should be at runtime i am creating variable a1 and assigning it value to msg . print "$msg\n" ; } above thing is not working !!! i want when k = 1... (4 Replies)
Discussion started by: zedex
4 Replies

5. Shell Programming and Scripting

Perl - setting a variable ?

hi there, I have a question about a snippet of code i have which runs localtime() to convert the current date/time into a mysql happy format my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); printf "%4d-%02d-%02d ... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

7. Shell Programming and Scripting

PERL : Use of a variable in a tr

Hi, I want to count the number of occurences of a character in a string variable ($str). The character is stored in a another variable ($sepchr). I am using tr as : $count = ($str =~ tr/$sepchr//); This did not work. I found in another thread about using eval. I used eval as : ... (13 Replies)
Discussion started by: sinpeak
13 Replies

8. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

9. Shell Programming and Scripting

Perl help - how to assign output of perl to variable

Hi, guys, i have a script i inherited from a coworker but i'm not perl savy. The script works but i would like it to work better. I want to run this command ./ciscomgrtest.pl -r "show version" -h hosts.router and have the script goto each router in the hosts.router file and run the command... (2 Replies)
Discussion started by: whipuras
2 Replies

10. Shell Programming and Scripting

PERL $0 variable

In PERL , $0 variable displays program name ( if we use inside script) .likewise is there a way to display program name and it's arguments passed to script . e.g. test.pl -a1 -b3 -c4 inside test.pl , if I use $0 , it gives me test.pl ..but I am looking for command to get complete program... (1 Reply)
Discussion started by: talashil
1 Replies
Data::Page(3)						User Contributed Perl Documentation					     Data::Page(3)

NAME
Data::Page - help when paging through sets of results SYNOPSIS
use Data::Page; my $page = Data::Page->new(); $page->total_entries($total_entries); $page->entries_per_page($entries_per_page); $page->current_page($current_page); print " First page: ", $page->first_page, " "; print " Last page: ", $page->last_page, " "; print "First entry on page: ", $page->first, " "; print " Last entry on page: ", $page->last, " "; DESCRIPTION
When searching through large amounts of data, it is often the case that a result set is returned that is larger than we want to display on one page. This results in wanting to page through various pages of data. The maths behind this is unfortunately fiddly, hence this module. The main concept is that you pass in the number of total entries, the number of entries per page, and the current page number. You can then call methods to find out how many pages of information there are, and what number the first and last entries on the current page really are. For example, say we wished to page through the integers from 1 to 100 with 20 entries per page. The first page would consist of 1-20, the second page from 21-40, the third page from 41-60, the fourth page from 61-80 and the fifth page from 81-100. This module would help you work this out. METHODS
new This is the constructor, which takes no arguments. my $page = Data::Page->new(); There is also an old, deprecated constructor, which currently takes two mandatory arguments, the total number of entries and the number of entries per page. It also optionally takes the current page number: my $page = Data::Page->new($total_entries, $entries_per_page, $current_page); total_entries This method get or sets the total number of entries: print "Entries:", $page->total_entries, " "; entries_per_page This method gets or sets the total number of entries per page (which defaults to 10): print "Per page:", $page->entries_per_page, " "; current_page This method gets or sets the current page number (which defaults to 1): print "Page: ", $page->current_page, " "; entries_on_this_page This methods returns the number of entries on the current page: print "There are ", $page->entries_on_this_page, " entries displayed "; first_page This method returns the first page. This is put in for reasons of symmetry with last_page, as it always returns 1: print "Pages range from: ", $page->first_page, " "; last_page This method returns the total number of pages of information: print "Pages range to: ", $page->last_page, " "; first This method returns the number of the first entry on the current page: print "Showing entries from: ", $page->first, " "; last This method returns the number of the last entry on the current page: print "Showing entries to: ", $page->last, " "; previous_page This method returns the previous page number, if one exists. Otherwise it returns undefined: if ($page->previous_page) { print "Previous page number: ", $page->previous_page, " "; } next_page This method returns the next page number, if one exists. Otherwise it returns undefined: if ($page->next_page) { print "Next page number: ", $page->next_page, " "; } splice This method takes in a listref, and returns only the values which are on the current page: @visible_holidays = $page->splice(@holidays); skipped This method is useful paging through data in a database using SQL LIMIT clauses. It is simply $page->first - 1: $sth = $dbh->prepare( q{SELECT * FROM table ORDER BY rec_date LIMIT ?, ?} ); $sth->execute($page->skipped, $page->entries_per_page); change_entries_per_page This method changes the number of entries per page and the current page number such that the first item on the current page will be present on the new page. $page->total_entries(50); $page->entries_per_page(20); $page->current_page(3); print $page->first; # 41 $page->change_entries_per_page(30); print $page->current_page; # 2 - the page that item 41 will show in NOTES
It has been said before that this code is "too simple" for CPAN, but I must disagree. I have seen people write this kind of code over and over again and they always get it wrong. Perhaps now they will spend more time getting the rest of their code right... SEE ALSO
Related modules which may be of interest: Data::Pageset, Data::Page::Tied, Data::SpreadPagination. AUTHOR
Based on code originally by Leo Lapworth, with many changes added by by Leon Brocard <acme@astray.com>. CONTRIBUTORS
James Laver (ELPENGUIN) COPYRIGHT
Copyright (C) 2000-9, Leon Brocard LICENSE
This module is free software; you can redistribute it or modify it under the same terms as Perl itself. perl v5.16.2 2013-08-25 Data::Page(3)
All times are GMT -4. The time now is 03:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy