Solved

shift the % array with removal of the first element

Posted on 2000-03-14
3
260 Views
Last Modified: 2010-03-05
I am doing the following

my(%array) = undef;

@array{@one) = @two;

After I assign like this I always have
the first element in %array (%array[0])
present but undefined.

I would like to shift it out.

Please help.

0
Comment
Question by:sstouk
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
fragtastic earned 50 total points
ID: 2616751
Hmmmmm

1. %array isn't an array, it's a hash;

2. If you want to undef something I'd use something like

undef @array;

It looks less like you are trying to assign something to @array.

3. Try changing your code to look like this. I'm not sure what it is you're trying to achieve, but this might be the way forward.

Are you trying to just create a variable at the start, or remove the value from one already in existence??

undef %hash;           # Use this to "erase" hash
my %hash;              # Or use this to create a local hash
$two = "Hello!!";
$hash{one) = $two;     # Now the key in %hash called one will have a value of whatever the scalar $two is. This was set earlier.

print $hash{one};      # This will print "Hello!!" to the screen. You are printing the value from %hash that has a key name of one;

So in smoother code.

my %colours;

$1 = "Blue";
$2 = "Red";

$colours{car} = $1;
$colours{truck} = $2;

print "My car is $colours{car}\n";
print "My truck is $colours{truck}\n";

Make sense? Or did you really want to use an array???


0
 
LVL 6

Author Comment

by:sstouk
ID: 2616791
Perfect Answer.
Thank You very much

the statement:
my %hash;

is all I needed.

Thaks again!
0
 
LVL 1

Expert Comment

by:fragtastic
ID: 2616797
Looked at the title of your question.

If you want to work with an array. Do this.

@array = (one, two, three, four, five, six);
print "Array is @array\n";
$first = shift(@array);
print "After shifting, array is @array\n";
print "and the element shifted out is $first\n";

If you're trying to shift elements out of a hash, don't. The data in a hash isn't stored in order like an array. When you take a slice of an array, it always returns the same value. A hash wouldn't as you use the key to access the value, not the position in the array. For example, using the above code

$array[0]   would have a value of one (or two if you look at it afer the shift) and would always have that value

It seems like you need to be clear on the differences between arrays and hashes and know when to use each one.

Perhaps you could explain in more depth what you are trying to do and post some example code so I can help more.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now